Mahedi Sabuj Mahedi Sabuj - 6 months ago 28
Javascript Question

Get Inner Text from DIV

HTML:

<div id="country">
<span>B</span>
Bangladesh
</div>

<div id="capital">
Dhaka
<span>D</span>
</div>



  1. From
    #country
    , I want to get Bangladesh

  2. From
    #capital
    , I want to get Dhaka



How can I achieve this?

Answer

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