Skip to content

Instantly share code, notes, and snippets.

@yaph
Created October 24, 2013 20:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yaph/7144195 to your computer and use it in GitHub Desktop.
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.
var list = [];
$('ul a').each(function(idx, item){
list.push({
href: $(item).attr('href'),
text: item.textContent
});
});
JSON.stringify(list, null, " ");
@Larceniii
Copy link

Thank you, what a fantastic snippet!

@diegooCF
Copy link

diegooCF commented Mar 7, 2019

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

@jervis446
Copy link

Thanks, Buddy you really saved my day!

@yaph
Copy link
Author

yaph commented Jun 27, 2019

@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