Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to copy the text inside an <a> tag and move it inside a specific <div>

Tags:

jquery

I have this piece of code:

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

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

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

I can copy the whole

  • but can't seem to figure out how to grab just the text. I'm using it to dynamically create a title to a side nav on my site.
  • Thanks for any help

    like image 373
    Mike Muller Avatar asked Jan 30 '26 17:01

    Mike Muller


    2 Answers

    You probably want to use .text():

    $('#sideNavHeader').text($('li.selected a').text());
    
    like image 129
    Tobias Cohen Avatar answered Feb 01 '26 08:02

    Tobias Cohen


    Tobias Cohen's selector was much better than mine you should use:

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

    instead of my original code:

    $('#sideNavHeader').html($('li.selected').children('a').html());
    
    like image 39
    BenMills Avatar answered Feb 01 '26 07:02

    BenMills



    Donate For Us

    If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!