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); } Link to comment https://forums.phpfreaks.com/topic/203293-syntax-issue/ 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> "; Link to comment https://forums.phpfreaks.com/topic/203293-syntax-issue/#findComment-1065136 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> Link to comment https://forums.phpfreaks.com/topic/203293-syntax-issue/#findComment-1065162 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>"; Link to comment https://forums.phpfreaks.com/topic/203293-syntax-issue/#findComment-1065164 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>"; Link to comment https://forums.phpfreaks.com/topic/203293-syntax-issue/#findComment-1065165 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. Link to comment https://forums.phpfreaks.com/topic/203293-syntax-issue/#findComment-1065170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.