Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery replace text leaving siblings intact
    primarykey
    data
    text
    <p>I'm having trouble wrapping my head around what should be a simple solution. I want to replace text within a label tag, without affecting the other 'siblings', if they exist.</p> <p><strong>Sample markup:</strong></p> <pre><code>&lt;fieldset class="myFieldsetClass"&gt; &lt;legend&gt;Sample Fieldset&lt;/legend&gt; &lt;ol&gt; &lt;li&gt; &lt;label&gt; &lt;span class="marker"&gt;*&lt;/span&gt; Some Label 1 &lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;label&gt; Some Label 2 &lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;label&gt; Text that doesn't match... &lt;/label&gt; &lt;/li&gt; &lt;/ol&gt; &lt;/fieldset&gt; </code></pre> <p><strong>Goal:</strong></p> <ul> <li>To replace text <code>Some Label X</code> with <code>Some Label</code> (I.e. remove <code>X</code> from the label text).</li> <li>span tags within label must stay intact if they exist, such as the <code>&lt;span class="marker"&gt;</code> above. </li> <li>I do not know the value of <code>X</code>, which could be 1 or more characters long.</li> </ul> <p><strong>Current Script:</strong></p> <p>My jQuery script below works, but I know is very inefficient. I can't seem to wrap my head around this one for some reason...</p> <pre><code>//for each label in the fieldset that contains text "Some Label " $(".myFieldsetClass label:contains('Some Label ')").each(function() { if ($(this).has("span").length &gt; 0) { //if the label has the span tag within, remove it and prepend it back to the replaced text $(this).find("span").remove().prependTo($(this).text(labelText)); } else { //otherwise just replace the text $(this).text('Some Label'); } }); </code></pre> <p>I thought at first I could simply do:</p> <pre><code>$(".myFieldsetClass label:contains('Some Label ')").text("Some Label"); </code></pre> <p>But this clears all contents of the label and hence removes the span, which I don't want. I can't use any replace functions to replace <code>Some Label X</code> with <code>Some Label</code> because I don't know what <code>X</code> will be.</p> <p>Can anyone suggest a more elegant/efficient approach to this problem?</p> <p>Thanks.</p> <p><strong>EDIT</strong></p> <p>After trying multiple answers, I think the problem seems to be that even if I select the right collection, they are text nodes, which jquery doesn't seem to want to modify.. I've used FireBug to select the collection (many answers below all select correctly but in slightly different ways). In firebug console resulting set is:</p> <pre><code>[&lt;TextNode textContent="Some Label 1:"&gt;, &lt;TextNode textContent="Some Label 2:"&gt;, &lt;TextNode textContent="Some Label 3:"&gt;, &lt;TextNode textContent="Some Label 4:"&gt;, &lt;TextNode textContent="Some Label 5:"&gt;] </code></pre> <p>The problem seems to be that calling .replaceWith(), .replace(), .text(), etc. doesn't seem to affect the jquery collection. If I allow the above collection to contain one of the spans, then calling .replaceWith(), .replace(), etc functions correctly against the span, but the text nodes stay as is..</p>
    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.
 

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