1

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?

6
  • print to the page or the console or print where? Commented Nov 11, 2013 at 15:37
  • how i can show this value in HTML page? Commented Nov 11, 2013 at 15:37
  • 1
    You can create a new text node, use innerHTML, innerText in selected browsers, document.write in selected environments, or use a library like jQuery. Take your pick. Commented Nov 11, 2013 at 15:39
  • Can you please give me JS viddle how the whole HTML and JS script will look like? Commented Nov 11, 2013 at 15:43
  • NOTE: There's no such thing as a "Jquery variable". It's a "JavaScript variable". Commented Nov 11, 2013 at 15:49

3 Answers 3

4

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

3 Comments

My <div id='duration'></div> must be before this scirpt? How the whole html file with the script will look like? Thanks in advance!
@user2979725 JavaScript is generally unlike how PHP or similar is used in HTML documents. You typically have a HTML page, and then the JavaScript runs afterwards and modifies the HTML as needed. So you would create your div element, and then right at the end of your document run JavaScript to populate it with data.
@user2979725 I assumed you already have a html page (where is myPlayer.duration() coming from?) The element #duration must be somewhere within the body. The script can be in the body at some point after the div or in the head.
1

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/

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.