Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving HTML tag from string with jQuery
    primarykey
    data
    text
    <p>I have a form with several checkboxes and labels, most of the functionality is working correctly, but I'm having trouble getting the label value for the checkbox, while stripping out a span element inside the label:</p> <pre><code>&lt;input type="checkbox" id="list-item-x" /&gt; &lt;label for="list-item-x"&gt;&lt;span class="date"&gt;Oct 1&lt;/span&gt; The checkbox label&lt;/label&gt; </code></pre> <p>I want the <code>label</code> element, but without the <code>span.date</code>. How can I remove this? I've tried the following:</p> <pre><code>$('label').remove('span.date').text(); </code></pre> <p>That didn't work, neither did the following:</p> <pre><code>$('label').find('span.date').text(); </code></pre> <p>How can I only remove the <code>span</code> element, leaving the <code>label</code> text alone for later usage?</p> <p>Edit: Here's the whole (messy) code:</p> <pre><code>$('input.list-item-checkbox').live('click', function(){ var myself = $(this); var my_id = $(myself).attr('id').split('-', 3); my_id = parseInt(my_id[2]); var my_label = $(myself).parent().children('label').html(); var parent = $(myself).parents('.list-item'); if ($(this).is(':checked')) { $.ajax({ type: 'POST', url: CI.base_url + 'ajax/unfinished_item/', data: 'list-item=' + my_id, dataType: 'json', success: function(result){ if (result.status == 'ERROR') { alert('Could not edit item'); } else { var html_string = '&lt;div class="list-item"&gt;' + ' &lt;input type="checkbox" class="list-item-checkbox" id="list-item-' + my_id + '" /&gt; ' + ' &lt;label for="list-item-' + my_id + '"&gt;' + my_label + '&lt;/label&gt;' + '&lt;/div&gt;'; $(myself).parents('.list-finished').siblings('.list-items').append(html_string); $(myself).parents('.list-item-finished').remove(); // This is where I need to get the span element removed from the label } }, error: function(){ alert('No contact with server'); } }); } }); </code></pre>
    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