13

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

4 Answers 4

52

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:

Sign up to request clarification or add additional context in comments.

2 Comments

can also do : originalString = originalString.replace('checkbox_','');
That was easy and quick thank you verymuch. I have another question though, can I accumulate all the checkboxes that have been selected and then send all those values as a single ajax call instead for each? I have Table.fnDraw() which does single ajax call for the each row selected
3
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();             

            });
        });

Comments

2

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

Comments

1

There are many ways to do it, some of them:

$("table :checked").each(function() {
    var theNumber = this.name.replace(/\D/g, "");
    var theNumber = this.name.replace(/[^\d]/g, ""); // or this
    var theNumber = this.name.match(/\d/g).join();   // or this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.