5

I have set a variable, and I need to pull in this variable into a html element, but I cant get it to print out the value, this is the code:

<script>
    var randomnumber=Math.floor(Math.random()*11)
</script>

<div id="<script type="text/javascript">document.write(randomnumber)</script>"></div>

Thanks.

Edit: I just used a div as an example, but i need to add a random number to an img tag, as it is for a tracking tag, and needs a unique identifier. Is there a better way to go about this?

4
  • 3
    What is this for? Why assign a random ID to an element? (Numeric-only IDs are invalid in HTML 4 btw) Commented Dec 10, 2012 at 18:09
  • 1
    Just inject the div via javascript itself. Btw in html4 you shouldn't have leading numeric id's Commented Dec 10, 2012 at 18:11
  • And I'm not even sure you can put script tags inside attributes... Commented Dec 10, 2012 at 18:11
  • 2
    document.write? In the end of 2012? Commented Dec 10, 2012 at 18:12

2 Answers 2

8

Use

<script>
document.write('<div id="'+randomnumber+'" ></div>');
</script>

You can't open a script tag inside an attribute

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

4 Comments

the issues with document.write() is that it doesn't execute before </body>. What if I needed the javascript to execute before?
@ArturGrigio I'm not sure of what you are saying. It does execute before </body>, it executes exactly where it on the page. So if you put it before some HTML it depends on, it would break because it wouldn't find the node (because it hasn't yet been parsed)
Lets say I had document.write('<img src="img.jpg">') when the page starts rendering, JS just writes <img src="img.jpg"> as plain text, until it reaches </body>, and then it renders as an image.
@ArturGrigio Nope that is incorrect, the image starts loading immediately. You can test it by analyzing the network, if the image is cached, it shows up immediately (add a breakpoint on a separate script tag). The HTML generated by document.write is immediately parsed by the browser once the script tag closes.
2

Or you can create it using JS:

var randomnumber=Math.floor(Math.random()*11);
a = document.createElement('div');
a.setAttribute('id',randomnumber);
document.body.appendChild(a);

// if you know the exact class or ID where it is to be appended you can use

document.getElementsByClassName("myclass")[0].insertBefore(a, document.getElementsByClassName("beforeClass").firstChild);

This will create a div, with the id as the randomnumber and append it to the body

7 Comments

I don't think you can be using appendChild here because the OP seems to want the div to be placed exactly where this code is. Your code will append the div to the end of the body, which probably isn't desired.
well you can find the specific class or div if you know the exact location, where you need to append it
@Ian calling document.body.appendChild(a); adds it as the last element at that moment, just like document.write would
@JuanMendes I just said that.
@Ian You said it will be the last thing in the body, that is only true if there is no HTML after the script tag. jsfiddle.net/WqbY7
|

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.