Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's your problem: In Javascript, <strong>strings are immutable</strong>, that means that they can't be changed. Unless you assign something different to the variable <code>temporaryString</code>, that variable isn't going to change.</p> <p>When you run this code:</p> <pre><code>temporaryString = "&lt;span&gt;Span Content&lt;/span&gt;&lt;div&gt;DIV Content&lt;/div&gt;"; $(temporaryString).find("div").remove(); </code></pre> <p>This is what happens:</p> <ol> <li><code>"&lt;span&gt;Span Content&lt;/span&gt;&lt;div&gt;DIV Content&lt;/div&gt;"</code> is assigned to <code>temporaryString</code></li> <li><code>$(temporaryString)</code> runs, parsing <code>temporaryString</code> and creating a corresponding array-like object (the jQuery object) of html elements and returns it. <code>temporaryString</code> is left unchanged.</li> <li><code>.find("div")</code> runs, finds all div elements in the result of the above step and returns them. <code>temporaryString</code> is left unchanged.</li> <li><code>.remove()</code> runs, removing the elements returned by the previous step from the jQuery object. <code>temporaryString</code> is left unchanged.</li> <li>The objects and elements created by <code>$(temporaryString).find("div").remove()</code> pass out of scope and are deleted by the garbage collector. <code>temporaryString</code> <strong>has still not been changed.</strong></li> </ol> <p>In short, if you don't do anything with the result of <code>$(temporaryString).find("div").remove()</code>, nothing happens.</p> <p>If you want <code>temporaryString</code> to become <code>"&lt;span&gt;Span Content&lt;/span&gt;"</code>, you need to assign the result of all the manipulations you've done to the jQuery object back into the <code>temporaryString</code> variable. The best way to do this is to create a <code>div</code> element, set it's contents to <code>temporaryString</code>, remove the desired elements, and assign the new contents of the <code>div</code> element back into <code>temporaryString</code> like this:</p> <pre><code>temporaryString = $("&lt;div&gt;").html(temporaryString).find("div").remove().end().html(); </code></pre> <p>For your original question, you can try this:</p> <pre><code>for(i = 0; i &lt; copyFromSchedule.length; i++) { temporaryString = $(this).attr("ows_"+copyFromSchedule[i]); if ($(temporaryString).html() == null) { /* Is not HTML... */ } else { // Is HTML, remove any anchors found in this string (&lt;a name="XXX"&gt;&lt;/a&gt;) temporaryString = $("&lt;div&gt;").html(temporaryString).find("a[name]").remove().end().html() } } </code></pre> <p>Sorry for the long winded answer.</p>
 

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