1

Here is my old code:

checkboxes = document.getElementsByName('foo');

As you know, checkboxes will be an array. Now I need to limit selecting scope. So this is my new code:

checkboxes = $('.myclass input[name=foo]');

But in this case checkboxes isn't an array anymore, it's a jQuery object. How can I make it the same as getElementsByName('foo')'s result?


Note that $('.myclass input[name=foo]')[0] won't work either.

asked Apr 10, 2017 at 4:49
7
  • Make sure all your checkboxes are inside an container whose class is myclass. Otherwise $('.myclass input[name=foo]')[0] will definitely work. Commented Apr 10, 2017 at 4:50
  • @RohanKumar Actually I've some other checkboxes which are out of the .myclass element and I don't want to select them. That's why I'm trying to use $('.myclass input[name=foo]') instead of document.getElementsByName('foo'). Anyway, my question is a totally different thing. Commented Apr 10, 2017 at 4:52
  • is .myclass also an input? Commented Apr 10, 2017 at 4:53
  • Can you show up your relevant code with html to solve your problem. You can create a Snippet for this too. Also, check that you have included a jquery version in your page. Commented Apr 10, 2017 at 4:54
  • 1
    Its SO, so every developer will definitely learn from this site. And you have many ways to do a single job in an optimised and even in a shortest way. Commented Apr 10, 2017 at 5:06

6 Answers 6

5

Try .toArray()

checkboxes = $('.myclass input[name=foo]').toArray();
Satpal
133k13 gold badges168 silver badges171 bronze badges
answered Apr 10, 2017 at 4:55

Comments

2

Use this

var checked = [];
$.each($("input[name='foo']:checked"), function(){
checked.push($(this). val());
});
answered Apr 10, 2017 at 4:55

Comments

2

Charlie already pointed out that jQuery objects have a toArray() function. That would work I think. Figured it was also worth noting that there is also a .makeArray() function for generally converting array-like objects to native JavaScript arrays. https://api.jquery.com/jQuery.makeArray/

answered Apr 10, 2017 at 4:58

Comments

2

You can use .map() and create an array of underlying DOM which getElementsByName() returns

checkboxes = $('.myclass input[name=foo]').map(function(){ return this;}).get();

I would recommend @Charlie answer


You don't need to convert to Node elements array. Change your function to

function toggle(source, target) {
 $(target).find('input[name=foo]').prop('checked', source.checked);
}

Usage

toggle(source, '.compare_people')
toggle(source, '.compare_posts')
answered Apr 10, 2017 at 4:53

2 Comments

It's nice of you recommending the answer of a person who has much less rep than you.
@Shafizadeh, Reputation at SO means nothing. Clearly he gave a better answer I totally forgot about that function
0

document.querySelectorAll("input[name=foo]")

answered Apr 10, 2017 at 5:16

Comments

0

Getting javascript object from jquery object by $('selector')[0] should work. See the answer from this link How to get javascript control from JQuery object? I suspect your selector is the reason why the above approach doesn't work. Add double quotes to name value will make it work:

checkboxes = $('.myclass input[name="foo"]');
answered Apr 10, 2017 at 6:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.