Dynamically setting div id in JavaScript or jQuery

后端 未结 8 1804
夕颜
夕颜 2020-12-15 11:43

How to set the div id dynamically? Here is my code:

<
8条回答
  •  有刺的猬
    2020-12-15 12:27

    First of all, you are giving the id to an element an id which is already given to some element before, that's not a right thing to do you can't have more than one id in your DOM. Anyway the code would be something like

        var element = document.getElementById('q4');
        element.setAttribute('id', 'q7');
        if(document.getElementById('q7').innerHTML = "All is well")
              alert(document.getElementById('q7').innerHTML);    //to check if the new id has been set correctly
    

    Fiddle http://jsfiddle.net/kmSHB/

提交回复
热议问题