You need to copy the text inside the <a> tag and move it inside the specific <div>

I have this piece of code:

<li class="selected"><a href="/AmIACandidate.html">Am I a Candidate?</a></li>

I need to take the text "Am I a candidate?" from inside the tag and move it inside the specific div tag.

<div id="sideNavHeader"> --Copy needs to go here-- </div>

I can copy everything

but I can’t figure out how to capture only text. I use it to dynamically create a title for a third-party navigator on my site.

Thanks for any help

+3
source share
2 answers

The Tobias Cohen selector was much better than mine, which you should use:

$('#sideNavHeader').html($('li.selected a').html());

instead of my source code:

$('#sideNavHeader').html($('li.selected').children('a').html());
+2
source

You might want to use .text():

$('#sideNavHeader').text($('li.selected a').text());
+3
source

Source: https://habr.com/ru/post/1735527/


All Articles