Bob
Bob

Reputation: 13

How to input a variable for the p id = "variable"

I have the following code

<span id="X1a">

<script>

document.getElementById("X1").innerHTML = 'Output1'
document.getElementById("X2").innerHTML = 'Output2'

<!---etc - several more similar lines for Outputx-->


</script>

What I want to do is to be able to input (or select) the value to put in the id= line. So i can choose to print X1 or X2 or Xx. How can I do that?

Upvotes: 1

Views: 143

Answers (1)

Kavin Sebastian
Kavin Sebastian

Reputation: 41

you can use setAttribute() to change the value id. example

document.getElementById("id").setAttribute("id", "X1")

or you can read reference in w3school

https://www.w3schools.com/jsref/met_element_setattribute.asp

Upvotes: 2

Related Questions