Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value from anchor tag using jQuery?

Tags:

jquery

<a href="" id="a1">myval</a>

How to get the value which is in between anchor tag ie; here "myval" and then specify that value to a hidden box.I tried do it with my piece of code but couldn't figure it out.pls help

<script type="text/javascript">
    $(document).ready(function(){
        $("#a1").click(function(e){
            //var str=$("#a1").getVal();        
            alert('hello');
        }
    });
</script>
like image 393
axhixh Avatar asked Mar 03 '11 11:03

axhixh


People also ask

How to get text value of anchor tag in jQuery?

To get the value of a script element, use the . html() method. As of jQuery 1.4, the . text() method returns the value of text and CDATA nodes as well as element nodes.

How do I retrieve all anchor elements in HTML?

To get all the anchor tags or links from the HTML document or the DOM, you can use the document. links property. You can get all the links using the document. links property using JavaScript.

How do I find my anchor tag?

To find anchor tag in div, use a selector and the addClass() method adds a class using jQuery.


4 Answers

var a1_text = $('#a1').text();
like image 182
kieran Avatar answered Oct 26 '22 06:10

kieran


var a_href = $("#a1").attr("href");

like image 39
Ujjwal Manandhar Avatar answered Oct 26 '22 06:10

Ujjwal Manandhar


You can use .text() to get the innertext of the anchor tag and .val() to store the value to a textbox.

$(function(){
    $("#a1").click(function(){
        $("#yourtextbox").val($(this).text());
    });
});
like image 20
rahul Avatar answered Oct 26 '22 08:10

rahul


<a href="" id="a1">myval</a>

you can use var yourValue= $('#a1').text(); it will works fine

like image 41
Krish Avatar answered Oct 26 '22 07:10

Krish