Alex
Alex

Reputation: 9720

Obtain Parent of HyperLink In JS method when I click on HyperLink

I have one HyperLink

<asp:HyperLink ID="link2" runat="server" 
                           NavigateUrl="javascript:MyMethod(this);" >
   Text
</asp:HyperLink>

on the the JS Method I want to receive this hyperlink

function MyMethod(item) {
//but parent of this item is window, but in html is li
                   debugger;
}

How to get the parent of this HyperLink on JS Method?

HTML In the page:

<li style="white-space: nowrap">
  <a id="ctl00_cphMain_ctl00_HyperLink2" href="javascript:MyMethod(this);">
                                                                Text Link</a>
  <input type="hidden" id="ctl00_cphMain_ctl00_id" 
                                  value="2g6e3c32-df3f-4533-sd3b-a99d6a79d9ea">
 </li>

Finally I want to get the value of hidden input, is the 2nd child of the li

Upvotes: 0

Views: 48

Answers (1)

T J
T J

Reputation: 43156

Change the markup to

<asp:HyperLink ID="link2" runat="server" onclick ="MyMethod(this);" > Text
</asp:HyperLink>

Then you can access the parent element using parentNode

function MyMethod(item) {
 var parent = item.parentNode; 
}

Upvotes: 2

Related Questions