Dynamically create divs with incremented id using jquery
Solution 1
Your title and question content seem to disagree with each other. I am assuming you are wanting to create div's where each id sequentially increments each time one is created?
$(function(){
var count = 0;
$('#append').click(function(){
$('#parent').append('<div id="first'+count+'">text</div>');
count++;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="append">Add DIV</a>
<div id="parent"></div>
Solution 2
You can change it with .attr():
$('#first').attr('id', 'first6')
Solution 3
try to change the code to :
<div id="first">text</div>
<div id="first0">text</div>
<div id="first1">text</div>
<div id="first2">text</div>
<div id="first3">text</div>
<div id="first4">text</div>
<div id="first5">text</div>
dont forget the id
inside " " (id="first"
not id=first
)
now you can simply use jquery : $("#first").attr('id','first6');
Solution 4
If you were interested in how styling works.`
$(function(){
for (let i = 0 ;i < 10; i ++){
$('#foo').append('<div id="first'+i+'">text</div>');
}
});
#first4 {
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo"></div>
subho das
I want to develop custom search directory using drupal. The home page will have one big Text search box & beside that one combobox which will contain list of data submitted by registered users. Also there would be search filter options at search result page. What would be the steps to start such application with Drupal? Thanks in advance..
Updated on July 12, 2022Comments
-
subho das almost 2 years
I have a text I want to append multiple divs but the id should be changed dynamically. e.g:
<div id=first>text</div> <div id=first0>text</div> <div id=first1>text</div> <div id=first2>text</div> <div id=first3>text</div> <div id=first4>text</div> <div id=first5>text</div>
Any help? thanks..
-
Syfer about 6 yearsJust what I was looking for. Mine is a little more complex but this will be a good start
-
Jabir Ishaq over 2 yearsthis made my day!