3

If I have a much simplified JavaScript method such as this:

function myfunction() {
     var i = 9;
}

Is there any way I can get the value of i into HTML such that when the web page with this method is called, the value 9 is displayed in a div or span element?

7 Answers 7

5

You can write document.getElementById("some ID").innerHTML = i

Sign up to request clarification or add additional context in comments.

Comments

1

Hi :D you can try the following using JQuery:

var i = 9;
$('#spanId').text(i);

or the classic old-fashion way:

var tmp= document.getElementById('spanId');
tmp.textContent = id;

I hope this helps.

2 Comments

-1: Forcing your users to download a 31kb library for such a basic task is ridiculous. For people still on dial-up, this represents a good 5-10 seconds download. Also, innerText/textContent should be used instead of innerHTML.
Thank you for pointing that out Andrew, I edited my answer. And yes, while it is ridiculous to download a library for such a basic task, I was just showing another way to do it :).
1

You can use innerHTML to embed the i value in a div or span element.

Comments

1

myfunction() current does not return its value. However, if you want to get the value when the page is "called" (loaded) you can do this:

function myfunction() {
    var i = 9;
    return i;
}

And in the markup:

<body onload="document.getElementById('id_of_your_div').innerHTML = myfunction()">

Please note that innerHTML has cross-browser issues, so you may want to use a library function such as jQuery's html() for reliable results.

9 Comments

-1: If you are not going to modify the HTML content of a tag, use innerText/textContent.
@Andrew Moore: innerText is completely unsupported in FF. quirksmode.org/dom/w3c_html.html
@George Commins: That's because FF uses the W3C standard .textContent property instead. My answer reflects that.
@Andrew Moore: This simply strengthens the argument for a library. It is a waste of a (good) developer's time to write conditional code for every available browser.
@George Cummins: We are talking about a single if statement between innerText or textContent. Between that and permitting injection (there is no information about the datatype returned by the OP's function... It is "much simplified", you can do the extra work for a simple if statement. That's the nature of vanilla-JS.
|
1

Yes, you can assign an id to your <div> or <span> and set its innerText/textContent property to your value.

window.onload = function() {
  var content = myfunction();
  var tag = document.getElementById('displayVar');

  if(typeof tag.innerText == "undefined") {
    tag.textContent = content;
  } else {
    tag.innerText = content;
  }
}

Do not use innerHTML if you do not want the HTML code of your value to be parsed (or if you don't expect any HTML value).

And no, you do not need a 31kb library to do that kind of work (just in case there's a bunch of "jQuery can do that!" answers).

Note that you must also modify myfunction() so that it returns the current value. A simple return i; statement in the function will do the trick.

1 Comment

Note that innerText is a proprietary feature with rather weird behaviour for all but the simplest cases, and that textContent is much more widely implemented.
0

JavaScript:

function myFunction() {
    var i = 9;
    return i;
}

$('myElement').innerHTML = myFunction();

HTML:

<span id="myElement"></span>

Comments

-1

you can also use jQuery to simplify the syntax following are the three ways you can do that using jQuery,

1) $('span').text(i)

2) $('#someid').text(i) //someid = value of id attribute of span tag.

3) $('.someclassname').text(i) //someclassname = class name for the span tag.

  • Also using jQuery means forcing the users have to download addition jQuery lib when the page loads. That might slow down the page depending on the connection speed of the users. You might want to consider that while selecting between jQuery and plain JavaScript

2 Comments

-1: Forcing your users to download a 31kb library for such a basic task is ridiculous. Also, before using jQuery, you should learn how vanilla JavaScript works. And since you are suggesting jQuery, you might as well to this the proper way and use .text().
Thanks Andrew! for pointing that out. I agree downloading the lib for such a task IS ridiculous. Obviously I missed a very important point.

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.