user2979725
user2979725

Reputation: 41

How to print one specific Javascript variable in HTML?

I want to ask how I can print a Javascript variable in HTML form (e.g. output it on the screen)?

Here is my JS code:

<script type="text/javascript">
var howLongIsThis = myPlayer.duration();
</script>

This var howLongIsThis = myPlayer.duration(); is Jquery variable!

So how i can show this value in HTML page?

Upvotes: 1

Views: 42439

Answers (3)

Ryan Williams
Ryan Williams

Reputation: 705

Assuming you have a <div id="example"></div> somewhere in your HTML, you want to run JavaScript after the DOM has loaded which adds the value you want to that div. In jQuery (which you specified in your question you're using), this would simply be:

$('div#example').html(myPlayer.duration());

Here's a fiddle: http://jsfiddle.net/RyanJW/QjXKL/2/

Upvotes: 1

Anand Jha
Anand Jha

Reputation: 10714

Try this,

your HTML

<input type="text" id="logId" />
<div id="logId1"></div>

js Script

var howLongIsThis = myPlayer.duration();
document.getElementById("logId").value=howLongIsThis;
document.getElementById("logId1").innerHTML=howLongIsThis;

hope this will give you an idea to solve your problem

Upvotes: 0

Moob
Moob

Reputation: 16184

Set it to be the innerHTML or value of a html element:

var howLongIsThis = myPlayer.duration();
var displayEl = document.getElementById("duration");
displayEl.innerHTML = howLongIsThis;

or if you want in in a form field:

displayEl.value = howLongIsThis;

Upvotes: 4

Related Questions