简体   繁体   中英

Return value from javascript to html

<head>
function productName(name)
{  
}
</head>

<body>
<img src="...images/car.jpg" onclick="productName('car')">
</body>

What I should write in this javascript function to print the value received from the onclick method to any public place in my html body?

Say you have an element like this:

<div id="content">

</div>

your js function would be like this:

<script type="text/javascript">
function productName(name)
{
    document.getElementById('content').innerHTML = name;
}
</script>

You can create a textNode and append it to the body

function productName(name) {
    var t=document.createTextNode(name);
    document.body.appendChild(t)
}

Demo: Fiddle

Or in jQuery,

$('#content').html( name);    // inner HTML of an element, by ID
$('#inputField').val( name);  // into an INPUT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM