When I user click on the button remove word and as enter 2 in the input box it should remove it from the element id=”list”
<input type="text" id="word"><input type="button" value="remove word">
<span id="list">1,2,3</span>
I did the following on the click event:
var text = $('.word').val();
$('#list').html($('#list').html().replace(/text/ig, ""));
I understand it looks for the string text and not the var. So how can I change the syntax to make it search for the var.
Literal regexps (
/.../) are literal in such a way that they cannot contain variables. You’d need to construct a regexp this way:Edit: If you need to have following commas removed, you’d be best off by parsing it as an
Arrayand removing the appropriate element: