2

i am generating a anchor from code behind and on its onclick function i want to call javascript function with string as a argument. i am trying this:-

 function show(data)
  {
        alert(data);
  }

and calling :-

 string data = "<a onclick='show(\""+Task+"\")' >" + anchor inner text + "</a>";

i am adding this string to a table cell and my project requirement is, when user click on that specific cell a popup show the content which i pass as an argument to javascript function.

6 Answers 6

2
string data = "<a onclick='show(\""+Task+"\")' >" + anchor inner text + "</a>";
Sign up to request clarification or add additional context in comments.

Comments

1

Much more cleaner approach would be storing that value in data-attribute:

<td data-message='Hello World!'>Click me!</td>

And replacing your function with jQuery:

$('td').click(function() {
    alert($(this).data('message'));
});​

See this DEMO.

Comments

0

I think the better way is to use RegisterStartupScript Method to do that.

StringBuilder sb = new StringBuilder();
sb.Append("<script type=text/javascript> alert('Hello World!') </");
sb.Append("script>");
cs.RegisterStartupScript(this.GetType(), "script", sb.ToString());

Comments

0

I think you missed one quote:

 string data = "<a onclick='show(\""+Task+"\") >" + anchor inner text + "</a>";

to

 string data = "<a onclick='show(\""+Task+"\")' >" + anchor inner text + "</a>";

Comments

0

You can use jQuery

$('td a').click(function(){

  var currLink=$(this);
  alert(currLink.text());//shows the inner text of anchor
  return false;//prevent default
});

Comments

0
 <a href='' onclick='return check(\""+Task+"\")'>check</a> 

<script type='text/javascript'>  

function check() 
{    
 alert(data); 
}  
</script> 

try Something like this. Code is not tested may get compile error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.