0

I have some checkbox inputs like so:

<input type="checkbox" name="1" class="filter"/>
<input type="checkbox" name="2" class="filter"/>
...etc...

I'm trying to write a function where any time a checkbox is selected, it generates a string with all the names concatenated. Here's what I have so far:

$('.filter').click(function(event){
    var filters = $('.filter').toArray();
    var fstr = "";
    for (f in filters)
    {
        fstr = fstr+","+f.name;
    }
    alert(fstr);
});

The names keep coming up as 'undefined', though (i.e. the alert returns ,undefined,undefined,undefined,undefined,undefined,undefined). How do I access the names?

7 Answers 7

4

Here's how it's meant to be done:

$('.filter').click(function (event)
{
    var fstr = '';
    $('.filter[name]').each(function ()
    {
        fstr += ',' + $(this).attr('name');
    });
    alert(fstr);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I was trying to figure out the proper jquery syntax, and what's more this one actually works :)
There is no .name() method, you have to get the name like any other attribute, using .attr(). You may have seen .val() used, but that is a special function primarily for getting the logical value of form elements.
1

You can use .map() to get what you're after, like this:

$('.filter').click(function(event){
  var names = $('.filter').map(function () { 
    return $(this).attr("name"); 
  }).get().join(',');
  alert(names);
});

Just change $('.filter') to $('.filter:checked') if you want a list containing only the checked ones.

Comments

1
$('.filter').click(function(event){
    var filters = $('.filter').toArray();
    var fstr = "";
    for (f in filters)
    {
        fstr = fstr+","+filters[f].name;
    }
    alert(fstr);
});

Why are you doing it that way anyway?

$('.filter').click(function(event){
    var str = '';

    $('.filter').each(function () {
       str += $(this).attr('name') +",";
    });

    alert(str);
});

Comments

1
(function($filters) {
    $filters.click(function(event) {
        var filters = $filters.toArray();
        var fstr = [];
        for (var i = filters.length - 1; i > -1; --i) {
            fstr.push(filters[i].name);
        }
        fstr = fstr.join(",");
        alert(fstr);
    }
})($('.filter'));

Comments

0

Try $(f).name.

Comments

0

How about $(elem).attr("name")?

2 Comments

By elem i mean any element. So in this case you'd use $(f)... - still undefined?
He's using for in, f is simply the property's key, not value.
0

I'm thinking the convert to array is your problem, try:

var filters = $('.filter');
for(var nI = 0; nI < filters.length; nI++)
filters.eq(nI).attr('name');

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.