Ad

Get Text Of Anchor Tag And Display It In Html Tag

- 1 answer

I'm using the below code and its working fine for me, for one anchor tag.

<h1 id="h1">heading</h1>
<a type="button" id="demo" onclick="Func()">sign-up</a>

JS

function Func() {
    document.getElementById("h1").innerHTML = "sign-up";
}

How to do this for 3 or 4 anchor tags ?

What I actually want is everytime when clicked on anchor tags get its text and display it in the only one <h1></h1> tag

Any answer will be appreciated either JS or JQuery

Ad

Answer

you should try JQuery like

your h1 tag like

<h1 id="anchorText"> </h1>

and your anchor tags like

<a type="button" id="demo">sign-up</a>
<a type="button" id="demo1" >Login</a>

Now in your jquery

$("a").click(function(){
    $("#anchorText").text($(this).text());
});
Ad
source: stackoverflow.com
Ad