I need to display the content from a DIV
name as sample
. Below the code I am trying, But it will return empty result.
HTML Code:
<div name="sample" >This is sample text</div>
<button id="getText" >Get External Content</button>
SCRIPT CODE:
$("#getText").click(function(){
var sample = $('div[name=sample]').val();
console.log("sample="+sample); //Empty result
});
Expecting Result:
sample=This is sample text
Thanks to all!
A div is a div, it has no value, thus .val()
is returning nothing.
Either change it to an input, or use .html()
or .text()
Use .text()
instead:
$('div[name=sample]').text();
.val()
jQuery method is used to get the values form the form
elements such as input, select, radio, checkbox
etc.
You have to use .text()
to get the textContent
of the div
element.
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.