locked
help needed in anchor tag RRS feed

  • Question

  • User1489758560 posted

    Hi,

    I am trying to construct anchor tag on the fly and below is my sample code. but it's not working.

    function GenerateLink(obj) {
    
      if(obj != null)
    {
            if (obj.Link != "" && obj.ImageUrl != "") {
                var newhtml = '<a href='" + obj.Link + "' + "onclick="javascript:GetValue(' + obj.userid + ')"';
                  
                newhtml= newhtml + '><div><img src="' + obj.ImageUrl + '" style="height: 50%; width: 20%;"/></div></a>'; 
                
            }
    }
        }
    
    function GetValue(value)
    {
      // some code here.
    }

    the newhtml should have constructed anchor tag and i will use that for some other purpose on the same code block. please help me in constructing the dynamic anchor tag with div and image in it. thanks in advance

    Monday, August 29, 2016 2:35 AM

Answers

  • User1724605321 posted

    Hi born2win,

    Please refer to my answer here :http://forums.asp.net/t/2102447.aspx?dynamic+div

    If you want to dynamic add onclick event to a element , you could refer to below thread :

    http://stackoverflow.com/questions/13133731/onclick-event-in-dynamically-created-button-in-javascript 

    Best Regards,

    Nan Yu 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 29, 2016 6:39 AM
  • User-474980206 posted

    you need to get the quotes correct and return the value

    function GenerateLink(obj) {
    
      if(obj != null) {
            if (obj.Link != "" && obj.ImageUrl != "") {
                return '<a href="' + obj.Link + '" onclick="javascript:GetValue(' + obj.userid + ')">' +
                       '<div><img src="' + obj.ImageUrl + '" style="height: 50%; width: 20%;"/></div></a>'; 
            }
       }
    }

    nor do you show how you plan to convert the html to the dom. also the userid better be a number or you need to quote it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 30, 2016 7:32 PM

All replies

  • User1724605321 posted

    Hi born2win,

    Please refer to my answer here :http://forums.asp.net/t/2102447.aspx?dynamic+div

    If you want to dynamic add onclick event to a element , you could refer to below thread :

    http://stackoverflow.com/questions/13133731/onclick-event-in-dynamically-created-button-in-javascript 

    Best Regards,

    Nan Yu 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 29, 2016 6:39 AM
  • User-474980206 posted

    you need to get the quotes correct and return the value

    function GenerateLink(obj) {
    
      if(obj != null) {
            if (obj.Link != "" && obj.ImageUrl != "") {
                return '<a href="' + obj.Link + '" onclick="javascript:GetValue(' + obj.userid + ')">' +
                       '<div><img src="' + obj.ImageUrl + '" style="height: 50%; width: 20%;"/></div></a>'; 
            }
       }
    }

    nor do you show how you plan to convert the html to the dom. also the userid better be a number or you need to quote it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 30, 2016 7:32 PM
  • User1489758560 posted

    thanks a lot guys.

    Friday, September 2, 2016 5:52 PM