0

I want to save the same object type in an Array()

For example when I want to select all checkboxes and save the value in an Array:

selectAllCbs: function() {
            var self = this;
            $('#' + instanceName + 'table_form input[type=checkbox]').each(function(index,cb) {
                if($(cb).prop('disabled') == false) {
                    $(cb).prop('checked',true);
                    console.log("Select all clicked:" + cb); //gives me [object HTMLInputElement] result
                    selected.push(cb); //save to an array
                    self.changeRowStyle(cb);
                }
            });

When I use almost the same code when user clicks on the row I'm getting different type:

rowClicked: function(id) {
        var cb = $('#' + instanceName + 'cb_' + id);

        if($(cb).is(':checked')) {
            $(cb).prop('checked',false);
            selected.splice(selected.indexOf(this.value),1);
        } else {
            $(cb).prop('checked',true);
            console.log("Row clicked:" + cb); //gives me [object Object]
            selected.push(cb);
        }
        this.changeRowStyle(cb);
    },

So the main question is how to save the rowClicked element in the [object HTMLInputElement] type?

Because in the first example I can read values with selected[i].value and with the second one I need to read it with the selected[i][0].value.

9
  • "Select all clicked:" + cb ...really !?... Commented Aug 1, 2013 at 11:05
  • @Virus721 whats wrong with that? That was just for testing purposes... Commented Aug 1, 2013 at 11:07
  • selected.push($(cb)[0]); Commented Aug 1, 2013 at 11:07
  • @dcodesmith in the select all or in the row clicked? Commented Aug 1, 2013 at 11:07
  • 1
    The question is: do you want to save the jquery object $(cb) or the DOM element cb ? Here var cb = $('#' + instanceName + 'cb_' + id); cb is a jquery object, a DOM element wrapped with jquery functions Commented Aug 1, 2013 at 11:09

2 Answers 2

1

In rowClicked, replace:

selected.push(cb);

With:

selected.push(cb[0]);

And do the same in rowClicked for this.changeRowStyle(cb[0]) method

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

3 Comments

He shouldn't need to do the same for the changeRowStyle method as the value passed from the each JQuery method is a Dom element. It hasn't been wrapped in the JQuery logic.
@shennan in rowclicked, he need to use this.changeRowStyle(cb[0]) as here cb is a jquery object
My apologies, I thought you meant change it in the selectAllCbs method, which is already a DOM element. Hence I mentioned the each JQuery method.
1

What may be confusing you is the comparison of your array elements at a later stage. You cannot compare two JQuery objects, but you can compare two DOM Elements.

JQuery wraps everything up as a JQuery object in order to super-charge it with all that sexy functionality. What you are storing in the first lot of code is a DOM element, because you don't wrap cb in the JQuery selector method. In the second lot of code, you're storing a JQuery selector.

In order to get the DOM element for the second lot of code, simply use the get method at the end of your rowClicked function:

this.changeRowStyle(cb.get(0));

And, obviously, change your push arguments for this function to be the same:

selected.push(cb.get(0));

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.