0

I would like some help with the following code.

What it's supposed to do is to create a button on a site that when pressed displays the same message, each time using one of the headers h6 to h1.

Here's the code.

<p>
<button type="button" onClick="headers()"> Show Headers</button>
<script> function headers() {
for(num = 6; num > 0 ; num--) {
  num.toString();
  document.write("<p>" + "<h(num)>" + "Message") <!-- That is the part that I cannot make to work. I wanted the num in the <hnum> to change as the for loop progresses so I can have h6 h5 h4...etc, dunno if it's possible to do it that way though :/  -->
  }
}
</script>
</p>

4 Answers 4

0

You were close! see this: https://jsfiddle.net/mm6ck3dc/

for(num = 6; num > 0 ; num--) {
  document.write("<p>" + "<h"+num+">" + "Message");
  }
Sign up to request clarification or add additional context in comments.

Comments

0

The loop you're trying to write should look like this:

for(var num = 6; num > 0; num--) {
  document.write("<h"+num+">" + "Message" + "</h"+num+">");
}

Also, there's no need for the heading element to be nested inside the <p> element.

Comments

0

Another EDIT: Actually you also need the end tag, and you won't need the <p> :

document.write("<h" + num + ">" + "Message" + "</h" + num + ">");

Comments

0

To understand Why I wrote \x3Cp instead of "<" read this: Why use \x3C instead of < when generating HTML from JavaScript?

for(num = 6; num > 0 ; num--) {
  num.toString();

  document.write("\x3Cp>" + "\x3Ch(num)>" + "Message")
}

2 Comments

Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
Ok i will try next time , Thanks For Your advice

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.