Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove a part of string within a string using jquery

Tags:

I'm developing a small app, when a checkbox is checked it's id value is added to a hidden field, when unchecked, it's id has to be removed from the hiddenfield value. I know how to add, but don't know how to remove. It's like say there is a string strValue="abcde"; how do I remove 'c' from the strValue using jQuery? Thank you.

like image 635
Steven Zack Avatar asked Aug 11 '11 17:08

Steven Zack


People also ask

How do I remove part of a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.


2 Answers

You can use replace.

strValue.replace('c', '');

Live Demo

I would suggest two things, looking at jQuerys .Data() method for storing values, and at the very least making your list delimited by something such as a comma.

like image 110
Loktar Avatar answered Oct 11 '22 21:10

Loktar


Try this, make sure you take the result of replace method into a new string. It does not modify the original string.

var str = "somestring";

str = str.replace('some', 'someother');
like image 42
ShankarSangoli Avatar answered Oct 11 '22 21:10

ShankarSangoli