Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert div ids to a string

I'm looking to do this with one line of code.

    var a = '';
    $('#MyDivs div').each(function (){a+=this.id+',';});
    a = a.substring(0,a.length-1);// remove the last comma

I though something like this would do it, but to no avail. Am I on the right tracks??

    $('#MyDivs div[id]').join(', ');
like image 677
JT... Avatar asked Jun 03 '26 20:06

JT...


2 Answers

Here you go in one line:

var a = $('#MyDivs div[id]').map(function(){ return this.id }).get().join(', ');

Here's the fiddle: http://jsfiddle.net/tGFeZ/

like image 57
Joseph Silber Avatar answered Jun 06 '26 10:06

Joseph Silber


This should offer some assistance: How to get all of the IDs with jQuery?

There is a one line example there, although it may not be the most attractive solution.

like image 25
jamesmillerio Avatar answered Jun 06 '26 08:06

jamesmillerio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!