0

Hey I have a problem with one loop: I need to put html into JavaScript because it is a meme. Here is the code which is working fine but using document.write. I don't want use document.write as it is not working on Firefox:

for (var m = 1; m < 5; m++) {   
  document.write("<li onClick=\"hello2()\"><a href=\"#\"><script>document.write(wynik)<\/script><\/a><\/li>");
}

I tried to use:

document.getElementById('menu').innerHTML = "<li onClick=\"hello2()\"><a href=\"#\"><Script>document.write(wynik)<\/script><\/a><\/li>");

But it is not working porperly. Wrong format and showing only once in the loop.

Any suggestions ?

4
  • Just out of curiosity, why are you having javascript write a script tag? Commented Feb 20, 2014 at 12:27
  • 1
    i'm not a true fan of manipulating HTML content to the DOM with .innerHTML. I prefer createElement(), appendChild(), removeChild() ... Commented Feb 20, 2014 at 12:29
  • 1
    why is this for "<Script>document.write(wynik)<\/script>"? you are already scripting then just concate variable "wynik" Commented Feb 20, 2014 at 12:30
  • you can removeTag your tag and .append what ever tag you want to be added Commented Feb 20, 2014 at 12:31

1 Answer 1

2
document.getElementById('menu').innerHTML += "<li onClick=\"hello2()\"><a href=\"#\">" + wynik + "<\/a><\/li>";

Use += and remove ) then it will get work..

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

13 Comments

That works.. But what about the second document.write which is inside this one ?? <Script>document.write(wynik)<\/script>
@user3324547 just use wynik direct, like '...<a href=\"#\">'+wynik+'<\/a>...'
Why you use document.write there ,Just write your variable only...That is wynik
wynik is result of some acitions before. It is variable.
what do you mean in clear html ? like that <li onClick="hello2()"><a href="#"><Script>wynik</script></a></li>. Its not working
|

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.