Solved

adding a hyperlink or href to a div

Posted on 2010-09-02
4
674 Views
Last Modified: 2013-11-19
I have a div with list of shopping cart items. When users click on "add to cart" button, i would like to add a hyperlink to href to the div.
The div itself shows on mouseover a shopping cart image that I show/hide using javascript.

Now, How can I 1) dynamically add the links to the div.
2) should the links be href or asp:hyperlink.

Please help. any suggetsion would be great. thanks a ton.
0
Comment
Question by:TrialUser
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 250 total points
ID: 33586571
you can do
var link = document.createElement("a");
link.setAttribute("href","");

document.getElementById("divId").appendChild(link);

or

document.getElementById("divId").innerHTML = document.getElementById("divId").innerHTML  + "<a href=''>";

0
 
LVL 13

Assisted Solution

by:ansudhindra
ansudhindra earned 250 total points
ID: 33586771
var link = document.createElement("a");
link.setAttribute("href","your url");

if link.setAttribute doesnot work then you can do
link.href="your url";

// to create a label to the link
var lb=document.createTextNode("Link Title");
link.appendChild(lb);

document.getElementById("divId").appendChild(link);

0
 

Author Comment

by:TrialUser
ID: 33587608
Ok , the above suggestion works great.  the code i used is as follows. only other thing is , I need like a <br /> after every link. how can i make sure the link is added in a newline each time. please help thanks

 function AddLink()
        {
            var link = document.createElement("a");
            link.setAttribute("href","Product.aspx");
            // to create a label to the link
            var lb=document.createTextNode("Product 1");
            link.appendChild(lb);
            document.getElementById("divCart").appendChild(link);
           
        }        
0
 

Author Comment

by:TrialUser
ID: 33587709
oh, i got it added a br. thanks a ton for your quick response.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to count occurrences of each item in an array.

831 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