0

I'm trying to delete a string from a textarea using jQuery once.

However, every time i run my code, I get the following error and nothing gets deleted from the textarea at all:

Error: Syntax error, unrecognized expression: <span data-price="5.99" data-name="Stamina" class="pricetag">Stamina</span>

To explain this better, I created this FIDDLE

If you click on the texts under the textarea, it should remove the string in the textarea but nothing happens.

This is my current code:

$(document).one('click', '.pricetag',function(){

 $("#Finalized").contains('<span data-price="5.99" data-name="Stamina" class="pricetag">Stamina</span>').remove();   

});

Could someone please advise on this issue?

EDIT:

I tried this and still doesn't delete anything from the textarea:

$(document).one('click', '.pricetag',function(){

// $("#Finalized").contains('<span data-price="5.99" data-name="Stamina" class="pricetag">Stamina</span>').remove();


    $('#Finalized').filter(function() {
    return $(this).html().indexOf('<span data-price="5.99" data-name="Stamina" class="pricetag">Stamina</span>') != -1;
}).remove();

});
H.HISTORY
  • 520
  • 9
  • 28

1 Answers1

1

is this what you're trying to achieve? JSFiddle

$(document).on('click', '.pricetag',function(){
   $("#Finalized").text($("#Finalized").text().replace('<span data-price="5.99" data-name="Stamina" class="pricetag">Stamina</span>',''));
});

if you want to replace all the occurrences, you can use regex for that, e.g. like THIS

Community
  • 1
  • 1
BeardedMan
  • 384
  • 2
  • 12