dizzy1 Posted May 29, 2010 Share Posted May 29, 2010 Hi, I always seem to manage to spend most of my time fighting syntax. I simply want to have an onclick which calls AddDrink and passes two variable into a string. But the onclick='AddDrink("+title+","+drink+")'; wont work. The Full Function: function getFunction(title,drink){ HtmlString = ""; HtmlString += "This is a Test"; HtmlString += " <a href='#' onclick='AddDrink("+title+","+drink+")'; >Add</a> <br> "; return(HtmlString); } Quote Link to comment Share on other sites More sharing options...
Alex Posted May 29, 2010 Share Posted May 29, 2010 If title and drink are strings they'll need quotes. HtmlString += " <a href='#' onclick='AddDrink(\'"+title+"\',\'"+drink+"\')'; >Add</a> <br> "; Quote Link to comment Share on other sites More sharing options...
dizzy1 Posted May 29, 2010 Author Share Posted May 29, 2010 It didn't word so I tried a basic alert which didn't work. Basic Alert: HtmlString += " <a href='#' onclick='alert('hello world')'; >alert test</a> <br>"; i then went to view source which displayed this: <a href="#" onclick="alert(" hello="" world')';="">alert test</a> Quote Link to comment Share on other sites More sharing options...
dizzy1 Posted May 29, 2010 Author Share Posted May 29, 2010 This kinda works now: HtmlString += " <a href=\'#\' onclick=alert(\'helloworld\'); >alert test</a> <br>"; but when I put a space in between helloworld it doesn't like: Doesn't Work: HtmlString += " <a href=\'#\' onclick=alert(\'hello world\'); >alert test</a> <br>"; Quote Link to comment Share on other sites More sharing options...
Alex Posted May 29, 2010 Share Posted May 29, 2010 Sorry, HtmlString += "<a href='#' onclick=\"AddDrink('" +title+ "','" + drink + "');\" >Add</a> <br>"; Quote Link to comment Share on other sites More sharing options...
dizzy1 Posted May 29, 2010 Author Share Posted May 29, 2010 Cheers that worked like a charm, ive spent so long trying to fight the syntax to get it to work. I guess the more I play with javascript the better ill get. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.