Remove string from string jQuery
javascript, jquery
Solution
This is just a JavaScript, not a jQuery, thing.
To remove the first occurence of the work "checkbox_":
var updatedString = originalString.replace("checkbox_", "");
Or if you know it'll always be in the form `"checkbox_n"` where `n` is a digit,
var updatedString = originalString.substring(9);
...which chops the first nine characters off the string.
In either case, you end up with a string. If you want a number, you can use `parseInt`:
var updatedString = parseInt(originalString.replace("checkbox_", ""), 10);
// or
var updatedString = parseInt(originalString.substring(9), 10);
...or just put a `+` in front of it to cause an automatic cast (but note that in that case, both decimal and hex strings will be handled):
var updatedString = +originalString.replace("checkbox_", "");
// or
var updatedString = +originalString.substring(9);
Note that I've written `updatedString = originalString.blah(...);` but of course you can replace your reference, e.g., "originalString = originalString.blah(...);`.
More to explore:
- MDN page on `String`
Problem
Ignore my novice knowledge in jQuery. I started to learn recently and I have a challenge in front of me. I have a checkbox with names checkbox_0, checkbox_1 and want to remove "checkbox_" from the strings so that I would use the 0, 1 in my loop to extract the data for that index. Thanks aData value alerts me the value checkbox_0, checkbox_1 etc. those are the checkboxes selected. ``` submitButton.on("click", function() { $("Table :checked").each(function(e) { var iData =Table.fnGetData( this.parentNode); // Strip of the checkbox_ from the string for(var i=0; i<=iData.length; i++) { aData = iData[i][7]; } alert(aData); Table.fnDraw(); }); }); ```