user2352951
user2352951

Reputation:

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?

Upvotes: 0

Views: 361

Answers (3)

Thomas W
Thomas W

Reputation: 14164

Or in jQuery,

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

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

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

Upvotes: 0

Kai Qing
Kai Qing

Reputation: 18833

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>

Upvotes: 4

Related Questions