Koiw
Koiw

Reputation: 17

array in jquery

i need to do array and then send in php file with help ajax. I have code:

$(function() {
    $('.photo-user-change').bind('click', function(){
        var hasclass = $(this).hasClass('active');
        if(hasclass == true) {
            $(this).removeClass('active');
        } else {
            $(this).addClass('active');
            var iwa = $('.active').attr('id');
            alert(iwa);
        }
    });
});

and html:

<ul>
  <li class="photo-user-change" id="1">1</li>
  <li class="photo-user-change" id="2">2</li>
  <li class="photo-user-change" id="3">3</li>
</ul>

When added class 'active' need introduce ID into an array and then sent this array in php file. ID maybe a few.

How do this? I need create multi selector. thanks.

Upvotes: -1

Views: 113

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 72961

Here's a quick way to get all elements with the class active and photo-user-change and create a comma separated list of their id attribute. This could easily be converted to JSON format - for your PHP web-service. You would place this right before you fire off the web service call.

var ids = $('.photo-user-change.active').map(function() {
    return this.id;
}).get().join(',');

alert(ids);

See it in action - http://jsfiddle.net/XSRUb/

Upvotes: 2

Related Questions