[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET modify attributes of <LI> in javascript and Codebehind

Posted on 2009-02-23
4
Medium Priority
?
532 Views
Last Modified: 2012-06-27
Given the html fragment below:

<UL>
                <li>
                <a href="sompage.html" target="_blank" >    Time Management</a>
                </li>
                <li>
                <a href="sompage2.html" target="_blank" >    Time Management</a>
                </li>
                <li>
                <a href="sompage3.html" target="_blank" >    Time Management</a>
                </li>
</UL>

How can I set the onClick event for each link? In javascript and in Codebehind.
0
Comment
Question by:Howard Bash
  • 3
4 Comments
 
LVL 39

Expert Comment

by:abel
ID: 23714451
In the code behind, you need to make the element server-side. You can do that by adding runat="server" to any control. Suppose here you can do:

<ul runat="server" id="myUl">
     <li runat="server"><a href="..." runat="server"
          ....

Now you can access myUl in the code-behind and set the children. Use .SetAttribute("onclick", "myjscode") for adding the onclick handler.
0
 
LVL 39

Expert Comment

by:abel
ID: 23714497
Through javascript it would be something like (assuming parent has ID, but no runat is set):



var objUL = document.getElementById('myUl');
   var objLI = objUL.firstChild;
   while(oChild) { // run over them
     if(objLI.nodeType==1 objLI.tagName == "li") {
           objLI.firstChild.setAttribute("onclick", "code");
     }
     objLI = objLI.nextSibling;
   }
}

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 23714501
Typo, should be:

var objUL = document.getElementById('myUl');
   var objLI = objUL.firstChild;
   while(objLI) { // run over them
     if(objLI.nodeType==1 objLI.tagName == "li") {
           objLI.firstChild.setAttribute("onclick", "code");
     }
     objLI = objLI.nextSibling;
   }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:Howard Bash
ID: 31550235
Just right.  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question