Solved

How to get text contained within <li>'s using jQuery

Posted on 2013-01-14
3
2,617 Views
Last Modified: 2013-01-14
Hello,  

For the following HTML I need to get the text contained within the <li> tags.  The html is created dynamically (& are the id's, it's a Salesforce thing)  so any number of <li> tag's may exist.


<div id="j_id0:j_id24:j_id361:j_id362:j_id365"><span id="j_id0:j_id24:j_id361:j_id362:j_id366"><span id="j_id0:j_id24:j_id361:j_id362:j_id366:j_id367:j_id368:0:j_id369"><div class="message errorM3"><table border="0" cellpadding="0" cellspacing="0" class="messageTable" style="padding:0px;margin:0px;"><tbody><tr valign="top"><td><img alt="ERROR" class="msgIcon" src="/s.gif" title="ERROR"></td><td class="messageCell"><div id="j_id0:j_id24:j_id361:j_id362:j_id366:j_id367:j_id368:0:j_id369:j_id370:j_id372" class="messageText"><span id="j_id0:j_id24:j_id361:j_id362:j_id366:j_id367:j_id368:0:j_id369:j_id370:j_id373" style="color:#cc0000"><h4>Errors</h4></span><br></div></td></tr><tr><td></td><td><span id="j_id0:j_id24:j_id361:j_id362:j_id366:j_id367:j_id368:0:j_id369:j_id370:j_id382"><ul style="padding-left:10px;padding-top:0px;margin:0px"><li style="padding-top:5px">j_id0:j_id24:j_id361:j_id362:j_id393:0:j_id395:1:j_id416:j_id417:input: Validation Error: Value is required.</li><li style="padding-top:5px">j_id0:j_id24:j_id361:j_id362:j_id393:0:j_id395:2:j_id416:j_id417:input: Validation Error: Value is required.</li></ul></span></td></tr></tbody></table></div></span></span></div>


I tried the following but it just returned "Errors" and not the rest of the text.

       alert('in messageText ' + $('.messageText').text());
          $('.messageText').each(function(index) {
              alert('in test123 ' + $(this).text());
           });

Does anyone know how I can search for each text value contained within the <li> tags?

Thanks in advance.
0
Comment
Question by:speedygonzalez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
WebDevEM earned 500 total points
ID: 38774887
I think this is what you're looking for:
$('.message li').each(function (index) {
  alert('in test123 ' + $(this).text());
});

Open in new window

This is selecting all LI that are inside the <div class='message'> and alerting on the text of them.
0
 
LVL 2

Expert Comment

by:tap52384
ID: 38774904
First, let me say that I recommend that you validate your HTML. You are using some deprecated attributes, like "cellpadding" and "border" and "valign." Use CSS instead.

To get the text of each <li>, try this:

$(".messageText li").eq(index).text();

where "index" is the index of the <li> you want the text of.
0
 

Author Closing Comment

by:speedygonzalez
ID: 38776488
Thanks for the help all..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question