I used a code that I found in the web and then changed a little thing:
<script type="text/javascript">
var foodList = [];
function addToFood (addFood) {
alert(addFood);
//foodList.push(addFood);
//for (i = 0; i < foodList.length; i++) {
// var newFood = "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
//};
//document.getElementById('foods').innerHTML += newFood;
}
</script>
At this moment I only want to alert the parameter on addToFood. The function should be called:
echo "<a href='javascript:addToFood(". $zeile1['TITLE'] .");' class='band-head'>Add</a>";
this line shows how I add an <a> with the javascript in the href. The php render the right line but the addToFood is never called.
Have a look at the online Demo: http://anthraxx.sytes.net/ maybe it can help you more then me.
The error I get via Google Chrome: Uncaught SyntaxError: Unexpected identifier But just can't figure that unexpected identifier out.
Thanks
You need to wrap the $zeile1['TITLE'] inside a string, as such:
echo "<a href='javascript:addToFood(\"". $zeile1['TITLE'] ."\");' class='band-head'>Add</a>";
You can see that I added escaped quotes \" after the opening parenthesis, and before the ending one, in the JS-call.
Otherwise it will try to pass an variable to the function, instead of a string.
Why escaped? That is because you are echo:ing it with PHP. If I didn't escape the quote, the PHP would interpret it as I was ending the string, which is not what we want. I want to put the quotes in there as inline-code.
Just scope your vaiable between ' ' or " " ,When you don't scope String's ,javascript read it like Var.
You need to escape the $zeile1['TITLE'], thats true and you can do that by using following line -
echo "<a href='javascript:addToFood(\"". $zeile1['TITLE'] ."\");' class='band-head'>Add</a>";
This is working.
try this
echo 'Add';
Replace :
echo "<a href='javascript:addToFood(". $zeile1['TITLE'] .");' class='band-head'>Add</a>";
With:
echo "<a href='javascript:addToFood(\"". $zeile1['TITLE'] ."\");' class='band-head'>Add</a>";
Related
i have jquery code like this, but when i click image share, its not working.
am i wrong about typing single quote ?
onClick="window.plugins.socialsharing.share('+my_var+')"
var my_var="";
my_var="Hello World";
$('#info_detail').append('<table border="0"><tr><td><img src="favourite.png" onClick="
set_favorit('+employee.info_id+')"></td><td><img src="share.png" onClick="
window.plugins.socialsharing.share('+my_var+')"></td></tr></table>');
thank you
The issue is because my_var is a string, therefore you need to wrap it in escaped quotes in the output. The same may be true for the employee.info_id value too:
$('#info_detail').append('<table border="0"><tr><td><img src="favourite.png" onClick="
set_favorit(\'' + employee.info_id + '\')"></td><td><img src="share.png" onClick="
window.plugins.socialsharing.share(\'' + my_var + '\')"></td></tr></table>');
You should however note that on* event attributes are very outdated and should be removed in favour of unobtrusive event handlers instead, like this:
$('#info_detail').append('<table border="0"><tr><td><img src="favourite.png" data-info-id="' + employee.info_id + '" class="favorit" /></td><td><img src="share.png" data-share="' + my_var + '" class="share"></td></tr></table>');
$('#info_detail').on('click', '.favorit', function() {
set_favorit($(this).data('info-id'));
}).on('click', '.share', function() {
window.plugins.socialsharing.share($(this).data('share'));
});
Your onClick for the share image is rendering incorrectly.
onClick="window.plugins.socialsharing.share('+my_var+')"
with my_var = "Hello World" becomes
onClick="window.plugins.socialsharing.share(Hello World)"
This is not only invalid because of the space, but also because it isn't being passed into as a string as you may be assuming. This can be remedied by adding single quotes to my_var = "'Hello World'". You may also be better off adding click handlers outside the HTML itself.
var my_var="Hello World",
employee = 'demo';
$('#info_detail').append('<table border="0"><tr><td><img src="favourite.png" data-info-id="' + set_favorit(employee) + '" class="favorit" /></td><td><img src="share.png" data-share="' + myVar(my_var) + '" class="share"></td></tr></table>');
function set_favorit(a) {
console.log(a)
}
function myVar(b) {
console.log(b)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="info_detail"></div>
As I don't have that window function, I have added some custom functions. Kindly check.
Try removing both single quotes from the statement I tried running the code removing the quotes it worked on my end.
Here is the code.
var my_var="";
my_var="Hello World";
var employee = {
info_id: 10
}
$('#info_detail').append('<table border="1"><tr><td><img src="favourite.png" onClick="set_favorit('+employee.info_id+')"></td><td><img src="share.png" onClick="alert(my_var)"></td></tr></table>');
Please help me to solve this string formating . it shows error while running.
I need to call a java script function AddHotel() with some php variables from an input tag. while running the first parameter in function shows error. It should be like onClick='AddHotel('divid', 'some_id', 'id',ids,rate)'
but while running in comes as onClick='AddHotel(divid', 'some_id', 'id',ids,rate)'
$resort[] = "<div id='".$iiiddd."'><input id='hotel_day".$child_post->ID.$dyid."' name='hotel_day".$dyid."' type='radio' value='".$child_post->ID."' onclick='AddHotel(".$p.",'".$s."','".$psid."','".$dyid."','".$child_post->ID."',".$child_post->fields['price'].")' />
<input id='".$s."' name='expsel".$dyid."[]' type='hidden' value='' />".$child_post->post_title."<span>Rs:- ".$child_post->fields['price']."</span></div>";
You want to escape Quotes
$resort[] = "<div id='".$iiiddd."'><input id='hotel_day".$child_post->ID.$dyid."' name='hotel_day".$dyid."' type='radio' value='".$child_post->ID."' onclick='AddHotel(".$p.",\'".$s."\',\'".$psid."\',\'".$dyid."\',\'".$child_post->ID."\',".$child_post->fields['price'].")' />".$child_post->post_title."Rs:- ".$child_post->fields['price']."";
For all who face such issues:
You should always use Double quotes for HTML attributes value eg:
<div attribute="value"><div>
For passing variables in a Javascript functions, you must use quotes (single quote preferably) for string. eg:
somethingAwesome('work', 'life', 1);
While embedding HTML and Javascript in PHP, you must not get confused with Quotes.
PHP string + HTML element:
$string = '<div></div>';
PHP string + HTML element with attributes:
$string = '<div id="awesome"></div>';
PHP string + HTML element with attributes + Javascript Function:
$string = '<div id="awesome" onclick="somethingAwesome()"></div>';
PHP string + HTML element with attributes + Javascript Function with Parameters:
$string = '<div id="awesome" onclick="somethingAwesome(\''.$string.'\', \''.$string2.'\', '.$integer.');"></div>';
You are open to choose double or single quotes, but following one particular fashion will prevent you from getting confused.
Also remember to Indent your code even in case of HTML in strings
Solution for your issue:
$resort[] = '<div id="'.$iiiddd.'">
<input id="hotel_day'.$child_post->ID.$dyid.'" name="hotel_day'.$dyid.'" type="radio" value="'.$child_post->ID.'" onclick="AddHotel(\''.$p.'\', \''.$s.'\', '.$psid.', \''.$dyid.'\', '.$child_post->ID.', \''.$child_post->fields['price'].'\')" />
<input id="'.$s.'" name="expsel'.$dyid.'[]" type="hidden" value="" />
'.$child_post->post_title.'
<span>Rs:- '.$child_post->fields['price'].'</span>
</div>';
I am generating dynamic HTML and want to pass an array to the calling function. The array is also being dynamically generated.
The contents of pinfoarray are somewhat like this "Ice Hockey","Junior Basketball","Ladies Soccer"
var theHost = "<a href='#someDialog' onclick='chnghost(' + pinfoarray + ');' data-toggle='modal' class='button'>Change</a>";
How can make it send the array to the calling function without an error.
How about making it like this:
var anchor = document.createElement('a');
anchor.href = '#someDialog';
anchor.setAttribute('data-toggle','modal');
anchor.className='button';
anchor.onclick = function(){
chnghost(pinfoarray);
}
You need to properly terminate the string and encode the parameter. You also need to guard against reserved characters in your data (escapeHtml).
var theHost = "<a href='#someDialog' onclick='chnghost(" + escapeHtml(JSON.stringify(pinfoarray)) + ");' data-toggle='modal' class='button'>Change</a>";
If you're using jQuery, you can implement escapeHtml like so:
function escapeHtml(text) {
return $("<div/>").text(text).html();
}
You need to close the string double quote to put a javascript variable:
var theHost = "<a href='#someDialog' onclick='chnghost('" + pinfoarray + "');' data-toggle='modal' class='button'>Change</a>";
I have this function into a javascript file :
function toggle_concessions(concessions) {
var text =
"<table>"+
"<tr><td class='concession-name'>gfhgfbfghfd</td><td class='op-encours'>| 15 opérations en cours</td></tr>"+
"<tr class='stats'><td class='concession-adresse'>ghfhdfhdgh</td><td class='voir-concessions'><img id='11' src='img/voirlesoperations.jpg' onclick='toggle_operations('ffff');'></td></tr>"+
"</table>";
;
if($("#"+concessions).attr("class")!="concessions toggled"){
$("#"+concessions).html(text);
$("#"+concessions).toggleClass("toggled");
}else{
$("#"+concessions).toggleClass("");
}
$("#"+concessions).toggle("slow");
}
The function "toggle_operations()" isn't working when I click on the image. Even when I execute an alert it doesn't work.
What can I do ?
Thanks
your html-embedded js is not well formed due to wrong pairing of string delimiters - you have to escape the quotes surrounding the argument to you toggle_operations call:
"<tr class='stats'><td class='concession-adresse'>ghfhdfhdgh</td><td class='voir-concessions'><img id='11' src='img/voirlesoperations.jpg' onclick='toggle_operations(\'ffff\');'></td></tr>"+
I need to pass a variable to a JavaScript function, but I have a little trouble. In the .cs file, I have written:
string id = "some'id";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
As you can see there is an ' (single quote) in the id. Is there any way to work around this issue?
Escape ' with a \ (backslash). For example,
console.log('string with \'');
Escape your string for such kind of characters"/","\","'"
example
string id = "some/'id";
You should escape your string , which would lead to :
id = "some\'id";
<script type="text/javascript">
function myFunction(someid) {
someid = someid.replace('#', '\'');
alert(someid);
}
</script>
in Your code
string id = "some'id".Replace("'","#");
this.Controls.Add(new LiteralControl("<input type=\"button\" value=\"Test\" onclick=\"myFunction('" + id + "');\">"));
Hope this will Helps you..