Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
I need id value of the anchor tag.It is not working.The value is coming undefined.
 
This my code:
HTML Code:
<a href="#" önclick="return listname1(this)" id="hollywood">A</a>
 
javascript code:
function listname1(list){
//alert(list.text);
var abc=list.text;
alert(abc);
 
var category=document.getElementById('hollywood').value;
alert(category);
// window.location="hollywood1.php?type="+abc;

}
Posted 27-Jun-12 22:30pm
Edited 27-Jun-12 22:36pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,
a tag has innerHTML property to get its text, try like this:
  function listname1(list) {
            //alert(list.text);
            var abc = list.innerHTML;
            alert(abc);
 
            var category = document.getElementById('hollywood').innerHTML;
            alert(category);
            // window.location="hollywood1.php?type="+abc;

        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This is working you can refer this and use:
function listname1(sender)
   {
       alert(sender.innerText);
   }
<a href="#" onclick="return listname1(this)" id="hollywood">A</a>
P.S. I hope i understood the problem correctly and the solution works for you if not do let me know.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use
var category=$('#hollywood').val();
Instead of
var category=document.getElementById('hollywood').value;
  Permalink  
Comments
tanweer akhtar at 28-Jun-12 3:44am
   
this will not work as the anchor tag has no val property
Member 8070578 at 28-Jun-12 3:52am
   
Thanks tanweer akhtar. It was my mistake.
So var category=$('#hollywood').innerHTML; should work.
tanweer akhtar at 28-Jun-12 3:57am
   
No, the correct jQuery code is
var category=$('#hollywood').html();
Deepthi.T at 28-Jun-12 5:04am
   
i tried all the solutions.Those are not working.
tanweer akhtar at 28-Jun-12 5:10am
   
did you check my answer? It is working as I tested it.
Deepthi.T at 28-Jun-12 5:21am
   
The value is coming as a.Id value is not coming.I got the solution.
 
var category=list.id;
alert(category);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100