HTML:
<div id="country">
<span>B</span>
Bangladesh
</div>
<div id="capital">
Dhaka
<span>D</span>
</div>
#country
, I want to get Bangladesh
#capital
, I want to get Dhaka
How can I achieve this?
To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.
The html(), text(), and val() are the most useful methods that can be utilized to get and set the inner text of HTML elements in jQuery. text() method gets and sets the content of HTML elements whereas, the html() method also sets its HTML markup.
The Div Object in HTML DOM is used to represent the HTML <div> element. This tag is used to specify the container for other HTML elements to style them with CSS or to perform certain tasks with JavaScript. Syntax: The <div> element can be accessed by using getElementById() method.
Try if this works
$('#country').clone().children().remove().end().text();
$('#capital').clone().children().remove().end().text();
.clone()
clones the selected element.
.children()
selects the children from the cloned element
.remove()
removes the previously selected children
.end()
selects the selected element again
.text()
gets the text from the element without children
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With