0

Just want two simple javascript vars to show up as text on the page. This code works for one variable but when I add a second call to getElementById it shows nothing:

html

 <p id="demo"></p>
 <span id="temper"></span>

JS

document.getElementById("demo").innerHTML = location;

document.getElementById("temper").innerHTML = temp_f;
6
  • 7
    what is <p2> tag ? Commented Apr 21, 2015 at 3:05
  • is there any error in the console... and which is the browser used Commented Apr 21, 2015 at 3:06
  • is your temp_f a declared variable? Commented Apr 21, 2015 at 3:08
  • Your code snippet will work fine in at least some browsers...as long as location and temp_f both have values. Do they? Commented Apr 21, 2015 at 3:08
  • 1
    In that case it should work fine. just make sure you run that code in window.onload. Can you share where you've defined the location and temp_f variable Commented Apr 21, 2015 at 3:32

1 Answer 1

2

Your problem is that you are trying to assign the variable location, which shadows the built-in:

var location = "x";

Instead, change the variable name to something else, for example, loc:

var loc = "x",
    temp_f = "y";

document.getElementById("demo").innerHTML = loc;

document.getElementById("temper").innerHTML = temp_f;
 <p id="demo"></p>
 <span id="temper"></span>

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

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.