0
<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?

1

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Can I call the value of the name in any place in html not in js?
0

Or in jQuery,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.