code_junkie

Extracting list data in jQuery

Nov 13th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. My List having
  2.  
  3. <li class="add"><a href="#">Data1</a></li>
  4. <li class="add"><a href="#">Data2</a> </li>
  5. ...
  6.  
  7. $(".add").click(function(e){
  8. e.preventDefault();
  9. // Here is to get the Value of Data1 and data2 ..
  10. // How to alert Data1,Data2 effectively
  11.  
  12. });
  13.  
  14. $('li.add a').each(function() {
  15. data.push($(this).text());
  16. });
  17.  
  18. $('.add').click(function(e) {
  19. e.preventDefault();
  20.  
  21. // Initialize an array for all texts
  22. var data = [];
  23.  
  24. // For each <li> with class 'add'...
  25. $('li.add').each(function() {
  26. // ...append the element's text to the data array
  27. data.push($(this).text());
  28. });
  29.  
  30. // Just for testing: Alert the array as a string
  31. alert(data.join(','));
  32. });
  33.  
  34. $('.add').click( function(e){
  35. e.preventDefault();
  36. data = [];
  37. //loop over each li:
  38. $('li.add').each( function() {
  39. //Get the a and the text contents of that
  40. data.push($(this).find('a').text());
  41. }
  42. alert(data.join(','));
  43. }
Add Comment
Please, Sign In to add comment