简体   繁体   中英

Get Only Text values in <li>

I have following html. Can someone help me in getting just the text value in <li> I only want the text value from all <span> within <li> within that <ul>

Thanks for help.

use .text() :

var id="JobMemo " + jobId + " <li>";
$.trim($("#"+id).text());

You can use .text() like

var jobMemo = $.trim($("#JobMemo\\ " + jobId).text());

Demo: Fiddle

Note: It is not a good idea to have space in the id....

You can try

$("[id='JobMemo " + jobId  + "']").text();

You should not use spaces, the reason is that, space character is not a valid for ID attribute.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

The working draft for HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

You should also read What are valid values for the id attribute in HTML?

In order to get the whole texts in an array you can do that

var jobId = '1';
var jobMemo = $("[id='JobMemo " + jobId  + "']")
.map(function() {
    return $(this).text();
}).get();

You can see : http://jsfiddle.net/8QTru/1/

jobMemo is an array with only the text of each li. But, like Satpal sayd, your ID is not valid. I suggest you to replace your space (" ") by a underscore ("_") in order to get something like that : id="JobMemo_1" instead of id="JobMemo 1".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM