开发者

How to get the value from anchor tag using 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="te开发者_运维技巧xt/javascript">
    $(document).ready(function(){
        $("#a1").click(function(e){
            //var str=$("#a1").getVal();        
            alert('hello');
        }
    });
</script>


var a1_text = $('#a1').text();


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


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());
    });
});


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

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


Try this :

$('#a1').click(function(){
   $(this).text();
})


 $("#a1").click(function(e){
           var str=$("#a1").html();        
            alert(str);
 });


"myVal" is not the link element's "value". It is the text that forms a child element inside it.

Use:

$(function(){
    $('#a1').click(function () {
        alert($(this).text());
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜