value is taken as parameter on function - javascript

im passing a value retrieved from an ajax call to a function like this way:
nuevaFila+='<td><input type="button" value="Agregar" onclick="AgregarSuvenir('+item.CODSUVENIR+');"></td></tr>';
and we i clicked the button it throws an undefinided "variable" but is not a variable, i just wanted to pass it as a string, and the value passed to the function is taken as a variable, i tried
onclick="AgregarSuvenir("'+item.CODSUVENIR+'") and onclick="AgregarSuvenir('+item.CODSUVENIR+""')
but this fails
someone help me how to pass the value as string and not be taken a a variable.
thanks is advance

This is a simple case of matching quotes. Because you use single quotes to delimit the string, you must add escaped quotes inside your function call in order for the inserted value to be treated like a string.
Here are two possible solutions that escape the quotations to allow for the parameter to the AgregarSuvenir function to be treated like a string:
nuevaFila+='<td><input type="button" value="Agregar" onclick="AgregarSuvenir(\''+item.CODSUVENIR+'\');"></td></tr>';
or:
nuevaFila+="<td><input type=\"button\" value=\"Agregar\" onclick=\"AgregarSuvenir('"+item.CODSUVENIR+"');\"></td></tr>";

found it!
it has to be formatted like stated on this post: pass string parameter in an onclick function
onclick="AgregarSuvenir(\''+item.CODSUVENIR+'\')

Related

Quotes escapes to correctly pass parameter from HTML input value to a javascript function from a PHP echo

I have a simple PHP echo statement which displays a HTML input box. I want to pass the data in value field when using the setTimeout function. I can't seem to get the quote escapes correct. This is my code snippets.
//PHP code
echo "<input type='text' id='myId' value='myVal' onkeyup='setTimeout(\"myFunction(this.value)\", 1000);'>";
How do I correctly pass [this.value] as a parameter to my myFunction
//Javascript code
function myFunction(val){
alert(val);
}
Thanks in advance
-M
You don't need any quotes there. The Javascript code should look like this:
setTimeout(myFunction, 1000, this.value);
You pass myFunction as function object as the first argument; this will be called by setTimeout. setTimeout also accepts further arguments which it will pass to the callback, so that's where you pass this.value. Since this doesn't involve any quotes, you don't need to worry about them.

pass String to function writing javascript

I'm trying to pass a string argument to JavaScript function.
for some reason, it can get only ints.
I put here only the syntax of the string, since I know that this is the problem because it worls with int.
Code inside sending function:
String counter = "hey";
out.println("<script>parent.update(\'' + counter + '\')</script>");
out.flush();
Eventually I'd expect following update function on my HTML page to be called with value of counter as shown above:
<script>
function update(p) { alert(p); }
</script>
as I mentioned, the javascript file does alert when I send an int, but doesn't react when I send a string.
What you are trying to do called "string interpolation" and used in other languages - you can have formatting string and get values automatically inserted to it.
The code you've used does not do that - since you pass just single string to out.println it is printed as is.
Your options
construct string with concatenation
out.println("<script>parent.update('" +
counter +
"')</script>");
use String.format as shown in String variable interpolation Java
out.println(String.Format("<script>parent.update('%s')</script>",
counter));
Note: if value you are trying to pass to JavaScript function may contain quotes (especially if coming from user's input) you'd need to escape it first.

cannot pass string to function

I'm using javascript and I try to pass a string to a function , like so
//example string
var f="a";
//add button that sends the value of f to the function
document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere("+f+");'> ";
function gothere(a){
alert(a);
}
I never see the alert and in console I see a is not defined (refers to the f I guess?)
If i set the f var to be a number then I see the alert.
What am I missing?
Thanks in advance
EDIT
I was thinking maybe something like
var buttonnode= document.createElement('input');
document.getElementById("mydiv").appendChild(buttonnode);
buttonnode.onclick=gothere(f);
Wont work for the same reason?
When your HTML get's rendered, you get onclick='gothere(a);', but the actual a variable doesn't exist in this context, you want to pass the value of f, as a string, so you'll need to use onclick='gothere(\""+f+"\");'. Note the extra quotes inside the parens. This will render to onclick='gothere("a");' thus passing the string.
When using a number, it works, because calling onclick='gothere(5);' is valid, since a variable can't be named 5, and it passes the number.
Actually, you don't have an a in your code. You are using variable f to denote a. So using this would help you:
var f="a";
// write the remains of the code as they are..
function gothere(f) {
alert(f);
}
Now when you'll call the function, there will be an alert of a in the browser.
Also, try wrapping the content in "" double qoutes to let the code understand that this is a string not a character.
For onclick use
onclick='gothere(" + f + ")'
And now, its onto you to write the value. Maybe the issue is because you're not writing the value for the f.
Try inpecting the error. I am sure there won't be anything.
Or try using the attribute field and change it using jQuery.
How about fixing your code ? You are missing the quotes around the value denoted by variable F.
Hence, when variable F is parsed, the function becomes gothere(a) . while a is not a defined variable (but its a value) and hence the error.
Try this !
document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere(\""+f+"\");'> ";
The modified part is onclick='gothere(\""+f+"\");'> "
This should work for you !
function parameter string value image dynamically from JSON. Since item.product_image2 is a URL string, you need to put it in quotes when you call changeImage inside parameter.
My Function Onclick inside pass parameter.
items+='<img src='+item.product_image1+' id="saleDetailDivGetImg">';
items+="<img src="+item.product_image2+" onclick='changeImage(\""+item.product_image2+"\");'>";
My Function
<script type="text/javascript">
function changeImage(img)
{
document.getElementById("saleDetailDivGetImg").src=img;
alert(img);
}
</script>
You need to use single quotation marks for value arguments (see #Nis or #CDspace answer).
Better way to handling dynamic clicks or other events is event binding. See jQuery event binding for example.

resolving single quote inside a variable

I have a javascript function call where my ${v.a} is getting
resolved to name like CNX'ion .so My call is failing because in the end it has three quotes like
<a href="javascript:editVendorDetails('${v.a}')// this changing into
<a href="javascript:editVendorDetails('CNX'ion')//have three quotes
can any one tell me how to get value of ${v.a} in my js function which may have quotes inside as explained above
use can use JSON.stringify('${v.a}')

JavaScript: Sending comma separated string of parameters to a javascript function

I am having a function in javascript as
function add(v1,v2){
var add=v1+v2;
}
Now I am calling this function as below -
write.out(var param="1,2";);
write.out(window[add](param););
Using the above call, it's not working. What it does is it gives the complete string "1,2" as value to the first param(v1) of the function.
Its working if I call the function in following way -
write.out(var param1="1";);
write.out(var param2="2";);
write.out(window[add](param1,param2););
I want to achieve it using the first way where i can send the parameters as a comma separated string of parameters.
Can some one help me out how this can be done...
Thanks!!!
You can make usage of ECMAscripts .apply(), which calls a function and accepts an array of paramters.
window['add'].apply(null, param.split(','));
That way, we execute the add function, setting its context to null (you could also change that if you need) and pass in the two paramters. Since we need an Array, we call split() on the string before.
So basically, the above line is the same as
add(1,2);
Since you're haveing that function in the global context (window), we don't even need to write it that explicitly.
add.apply(null, param.split(','));
will just be fine.

Categories