Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of using regex, parse it and loop through elements. Try:</p> <pre><code>var data = "&lt;div id='1'&gt;&lt;/div&gt;&lt;div id='asdf'&gt;&lt;input type='text' id='2'/&gt;&lt;/div&gt;", numCompare = 23, div = document.createElement("div"), i, cur; div.innerHTML = data; function updateId(parent) { var children = parent.children; for (i = 0; i &lt; children.length; i++) { cur = children[i]; if (cur.nodeType === 1 &amp;&amp; cur.id) { cur.id = cur.id + "_" + numCompare; } updateId(cur); } } updateId(div); </code></pre> <p><strong>DEMO:</strong> <a href="http://jsfiddle.net/RbuaG/3/" rel="nofollow">http://jsfiddle.net/RbuaG/3/</a></p> <p>This checks to see if the <code>id</code> is set in the first place, and only then will it modify it.</p> <p>Also, it is safe in case the HTML contains a comment node (where IE 6-8 does include comment nodes in <code>.children</code>).</p> <p>Also, it walks through all children of all elements. In your example, you only had one level of elements (no nested). But in my fiddle, I nest the <code>&lt;input /&gt;</code> and it is still modified.</p> <p>To get the get the updated HTML, use <code>div.innerHTML</code>.</p> <p>With jQuery, you can try:</p> <pre><code>var data = "&lt;div id='1'&gt;&lt;/div&gt;&lt;div id='asdf'&gt;&lt;input type='text' id='2'/&gt;&lt;/div&gt;", numCompare = 23, div = $("&lt;div&gt;"), i, cur; div.append(data); div.find("[id]").each(function () { $(this).attr("id", function (index, attr) { return attr + "_" + numCompare; }); }); </code></pre> <p><strong>DEMO:</strong> <a href="http://jsfiddle.net/tXFwh/5/" rel="nofollow">http://jsfiddle.net/tXFwh/5/</a></p> <p>While it's valid to have the <code>id</code> start with and/or be a number, you should change the <code>id</code> of the elements to be a normal identifier.</p> <p>References:</p> <ul> <li><code>.children</code>: <a href="https://developer.mozilla.org/en-US/docs/DOM/Element.children" rel="nofollow">https://developer.mozilla.org/en-US/docs/DOM/Element.children</a></li> <li><code>.nodeType</code>: <a href="https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType" rel="nofollow">https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType</a></li> <li><code>jQuery.find()</code>: <a href="http://api.jquery.com/find/" rel="nofollow">http://api.jquery.com/find/</a></li> <li><code>jQuery.attr()</code>: <a href="http://api.jquery.com/attr/" rel="nofollow">http://api.jquery.com/attr/</a></li> <li><code>jQuery.each()</code>: <a href="http://api.jquery.com/each/" rel="nofollow">http://api.jquery.com/each/</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload