最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - how to pass the value of <a> tag to a javascript function - Stack Overflow

programmeradmin0浏览0评论

First of all my question is, does a tag in html stores a value like input type = "text" does?

Secondly, I have a links like this let say:

<a href="#" >A</a>
<a href="#" >B</a>
<a href="#" >C</a>

I want to pass for each of the link their value let say A, B, C.

What i do is this:

  <a href ="" onClick = "sendVal(this);">A</a>
  <script type="text/javascript">
    function sendVal(letter){
      alert(letter);
    }
  </script>

But unfortunatelly i dont get A , but i get its href, how would i get the letter?? Any idea?

First of all my question is, does a tag in html stores a value like input type = "text" does?

Secondly, I have a links like this let say:

<a href="#" >A</a>
<a href="#" >B</a>
<a href="#" >C</a>

I want to pass for each of the link their value let say A, B, C.

What i do is this:

  <a href ="" onClick = "sendVal(this);">A</a>
  <script type="text/javascript">
    function sendVal(letter){
      alert(letter);
    }
  </script>

But unfortunatelly i dont get A , but i get its href, how would i get the letter?? Any idea?

Share Improve this question asked Jun 12, 2013 at 13:10 LulzimLulzim 9157 gold badges18 silver badges28 bronze badges 3
  • Must be pure JS or can you use Jquery ? – Neta Meta Commented Jun 12, 2013 at 13:11
  • How do I use Jquery with that, i am not familiar with jquery at all :s – Lulzim Commented Jun 12, 2013 at 13:13
  • 1 Who upvoted this highly-Google-available question? – Evan Davis Commented Jun 12, 2013 at 13:17
Add a ment  | 

3 Answers 3

Reset to default 3

try this.

<a href ="#" onClick = "sendVal(this.innerHTML);">A</a>

  function sendVal(letter){
     alert(letter);
  }
<a href ="#" onClick = "javascript: sendVal(this.innerHTML);">A</a>

here you go as JS only

<a href ="" onClick = "sendVal(this);">A</a>

function sendVal(letter){
    alert(letter.text);
}

If you want cross browser patibility i would really use jquery, otherwise you'll have to do lots of checks to see which to use.

If you are not limited to JS(as per client request) and this is a learning project you should really look at: jQuery

发布评论

评论列表(0)

  1. 暂无评论