I’ve got a button and when it’s pushed this code launches:
var nrDivs = “1”
var neuDiv = document.createElement(“div”);
neuDiv.setAttribute(“Id”,”theDiv” + nrDivs);
neuDiv.innerHTML = “Text”;
$(‘#allDivs’).append(neuDiv);newfeed.draw(neuDiv);
I know that right now the script creates a new div with the ID: theDiv1.
What I’m trying to do is when I click the button a second time, it creates a div with the ID: theDiv2.
Each time the user presses the button there should be a new div drawn and I’m trying to target that new div and insert dynamic text.
Thanks, Noor
There’s more jQuery you could use here, but I kept it pretty much the same as yours, adding only enough to do what you wanted.
Please note that this will count ALL
divelements within#allDivs. If there are some that you don’t wish to count, another approach must be taken.EDIT:
A more jQuery-like way:
EDIT:
Selectors in jQuery are very powerful and offer lots of flexibility.
For example, the line above that gets all the
divelements under#allDivscould be re-written as:… which is exactly the same.
If you only want to get the
divelements that are immediate children of#allDivs, use:or
Another alternative would be to give these
divelements a special class that you can use as a selector. So, for example, when you create a newdivto add to#allDivs, do it this way:Now the top
divelements in#allDivswill have a class called `topDiv’, and you can reference that in your selector like this:…which will only return
divelements that have the classtopDiv.This only scratches the surface of what you can do with selectors in jQuery.
Hope this helps.