I have a string which holds a small amount of HTML generated content. I want to add an ID to the first tag inside it. To make it clearer:
I want a function that does this.
It uses this:
<li><img src=... /></li>
And makes it into this
<li id="__VariableValue__"><img src=... /></li>
However, this is not a live element yet, it is all inside a string. The first tag is not fixed, it is supplied dynamically, so i cannot use a offset of 3, as it could be a <table>, <a> etc…
Any suggestions?
Anyone looking for the answer: Its here:
var Element = $(Element).attr('id',myID);
You tagged jQuery, so it’s as simple as:
This creates the elements defined in
yourString, but does not attach them to the DOM. I assume you’d end up using something along the lines of: