How do I get the id of a hyerlink on a page?
Example
<a id="myID123" href="myLinkPageName.aspx">myLink</a>
Note: The page name and the link name is static! I should get the id "myID123".
How do I get the id of a hyerlink on a page?
Example
<a id="myID123" href="myLinkPageName.aspx">myLink</a>
Note: The page name and the link name is static! I should get the id "myID123".
walk through your A-Tags and look for matching href, then return the id
i assume u are using jquery, as we all do :-)
var foundid = "id not found";
var desired_href = "myLinkPageName.aspx";
$('a').each(function(){
if($(this).attr('href') == desired_href) foundid = $(this).attr('id');
});
alert(foundid);
this solution isn't pretty, but quick
use jquery is very easy
$('a').attr('id')
$("a[href='myLinkPageName.aspx']").attr('id')
You can give a class at the hyperlink you might want like
<a id="myID123" href="myLinkPageName.aspx" class="my-links">myLink</a>
and then search for it with jQuery doing the following:
$('.my-links').attr('id');
In case you want to get the ids for all your hypelinks in your page you can do the following:
$('a').attr('id');
You can also do more complex search using the following attributes:
= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains
An example might be:
$('a[href*="myLinkPageName"]')
Non jQuery solution, just for fun
var href_search = "myLinkPageName.aspxmyLinkPageName.aspx";
for (var i; i<document.links.length; i++) {
if (document.links[i].href == href_search) break;
}
var id = document.links[i].id;