Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need id value of the anchor tag.It is not working.The value is coming undefined.

This my code:
HTML Code:
HTML
<a href="#" önclick="return listname1(this)" id="hollywood">A</a>


javascript code:
JavaScript
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
Updated 27-Jun-12 21:36pm
v3

hi,
a tag has innerHTML property to get its text, try like this:
JavaScript
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;

      }
 
Share this answer
 
This is working you can refer this and use:
JavaScript
function listname1(sender)
   {
       alert(sender.innerText);
   }

XML
<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.
 
Share this answer
 
Use
JavaScript
var category=$('#hollywood').val();

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900