Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove string from string jquery

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();             

            });
        });
like image 411
user525146 Avatar asked May 14 '12 16:05

user525146


3 Answers

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
like image 184
T.J. Crowder Avatar answered Sep 25 '22 14:09

T.J. Crowder


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].replace("checkbox_", "");
                }
                alert(aData);
                Table.fnDraw();             

            });
        });
like image 44
rabbidrabbit Avatar answered Sep 24 '22 14:09

rabbidrabbit


To remove the checkbox_ part, you can simply do this:

cbName=cbName.replace("checkbox_", "");

To do this for all your checkboxes inside the .each() loop:

var cbIndex=this.name.replace("checkbox_", "");
//or...
var cbIndex=this.name.split("checkbox_").join("");
like image 43
Ashley Strout Avatar answered Sep 25 '22 14:09

Ashley Strout