Created
October 24, 2013 20:14
-
-
Save yaph/7144195 to your computer and use it in GitHub Desktop.
Convert an HTML ul list of links to a JSON list of objects with href and text content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = []; | |
$('ul a').each(function(idx, item){ | |
list.push({ | |
href: $(item).attr('href'), | |
text: item.textContent | |
}); | |
}); | |
JSON.stringify(list, null, " "); |
Thanks, Buddy you really saved my day!
@diegooCF yes idx
is how I abbreviate index
. It is not used in this snippet, that's just how the callback for each
is defined in jQuery, see https://api.jquery.com/jQuery.each/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much! Really helped me.
I have one question, i'm learning jQuery and i can't deduce what is the function of the idx (index?) parameter. Regards