Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I do not speak much English
But I learned
I have a question
How do we add a div with javascript increased its IDI value
For example:
Java
<script type="text/javascript">
var x=0;
 $("#btn").on("click",function(){
	 
	 x++;
 });
</script>

<div id="javascript:x">
</div>
<input name="btn" type="submit" value="Submit" />
Posted
Comments
bcsilent 15-Apr-13 9:09am    
I want to increase the value of X

You can set the ID of your element like this:
var elem = $('#' + x);
x++;
elem.attr('id', x);
 
Share this answer
 
Do you want to add a new div or change the id of the div on button click
if you want to change id of the div, do like this...
HTML
<div id="javascript:x">
</div>
<input name="btn" type="button" value="Submit" onclick="setIDs()" />


JavaScript
 <script type="text/javascript">
        var x = 0;
//        $("#btn").on("click", function () {

//            x++;
//        });
        function setIDs() {
//            debugger;
            var divs = document.getElementsByTagName('div');
            for (var i = 0; i < divs.length; i++) {
                divs[i].id = x++;
            }
        }
</script>


it changes the id of the div on each button click
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900