Passing Alphanumeric Parameter to javascript function - javascript

when i call a javascript function say, onclick="doSome(this.value)" where this.value=123, it works fine.
But if i call onclick="doSome(this.value) where this.value=A123, it is not working. How to rectify this problem?

I was also facing the same issue. I had to call a function with a product code as a parameter . It was working when the product code is numeric and it was not working for products like "AE11002".
I fixed the problem by changing the method invocation from
onClick="decreaseQuantity(${product.code})" to onclick='decreaseQuantity("${product.code}")'.
I think this will help you also. Please try this.

Related

string inside php inside html inside javascript

i've searched on the site but i could find nothing that could help me.
I have a javascript code inside html inside php, i would like to know how i can put a string inside the javascript code with "breaking" the href with double quotes. Here is the code i have:
return '<li class="slide_li" onmouseover="gmarkers['+marker_num+'].setIcon(getMarkerImage("grey", price, cur));" onmouseout="gmarkers['+marker_num+'].setIcon(gicons.blue)">'+details+'</li>';
The "grey" value breaks the href, i tried different ways like:
'grey', \'grey\', \"grey\" ecc.. but none of this worked for me. Any help is really appreciated. Thanx!
To avoid such pain in the future, I am suggesting you my way of doing things in such scenarios.
Since you are already using jQuery library, why not to use its way of handling events:
$('li.slide_li').on({
mouseover: function(e){
},
mouseoutleave:function(e){
});
instead of using inline javascript. This will make you away from dealing with quotes. Additionnaly, by seperating the concerns this way, you make your php return only markup.
About additional variables (like the variable containing 'grey'), I suggest you use data attributes, something like:
return '<li class="slide_li" data-color='grey'></li>
in javascript:
var color= $('li.slide_li').data(color);
// use color variable in code inside mouseover and mouseout
I am not telling here that the other answers are not adressing your current problem, I am only sharing a way I was told in the past it is a better way.
You need to wrap your javascript variable with additional single quote and then you need to used backslash like below:
return '<li class="slide_li" onmouseover="gmarkers[\''+marker_num+'\'].setIcon(getMarkerImage("grey", price, cur));" onmouseout="gmarkers[\''+marker_num+'\'].setIcon(gicons.blue)">'+details+'</li>';
I tried with this "grey"
and it works!
Thank you everybody!
Try setting the variable outside of the javascript first:
var variable="grey"
return '<li class="slide_li" onmouseover="gmarkers['+marker_num+']
.setIcon(getMarkerImage(variable, price, cur));" onmouseout="gmarkers['+marker_num+']
.setIcon(gicons.blue)">'+details+'</li>'
;

ajaxToolkit PopupControlExtender not working. Outdated?

I'm followed this tutorial but I'm getting runtime error when mouseover:
Sys.ArgumentUndefinedException: Value cannot be undefined. Parameter
name: type
The problem is in this lines of code:
string OnMouseOverScript = string.Format("$find('{0}').showPopup();", behaviorID);
string OnMouseOutScript = string.Format("$find('{0}').hidePopup();", behaviorID);
img.Attributes.Add("onmouseover", OnMouseOverScript);
img.Attributes.Add("onmouseout", OnMouseOutScript);
Any thoughts on this? My goal is to get details of gridview row when mouseover a specific column, like the demo on the referenced linked.
Solved by updating AJAX Control Toolkit to it's last version (v15.1 from March 2015)!

Passing Javascript Object to Function & changing text field

This is probably a stupid mistake that i have made; i am still new to web development so be nice please :)
Here i create the object
var crs0 = {ID:1, TITLE:"test", DESC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",ER:"a",LENGTH:"a", FEE:"a"};
Here i use an onclick event to call a function & pass the object as a parameter
<div class = "btnDC" onclick="display(crs0)">test</div>
Here is the function that i use to replace data in some textarea's & text inputs with properties from the object.
function display(crs)
{
document.getElementById("ttl").value=crs.TITLE;
document.getElementById("dsc").value=crs.DESC;
document.getElementById("er").value=crs.ER;
document.getElementById("lng").value=crs.LENGTH;
document.getElementById("fees").value=crs.FEE;
document.getElementById("ID").value=crs.ID;
}
The onclick does nothing & i have no idea why. (Other javascript on the page does work so i haven't missed a semi-colon :D )
[Update 1]
All of the data is pulled from a database the code above is copied from the page it produces; i have done a few tweaks & i can get it to produce an alert box for the display function however if i try & make it show any data of the object within that alert box it doesn't display anything (i hate not having a debugger), which suggests that the object isn't being passed.
Here is the PHP code i use to create the onclick
echo '<div class = "btnDC" onclick="display(crs'.$n.')">'.$inf['TITLE'][$n].'</div>
Could that be the issue?
it produces this line of code
<div class = "btnDC" onclick="display(crs0)">test</div>
As mentioned the code i have shown works (thanks juvian);
I generated this code from php & although the javascript generated was correct there was a problem with some of the php, i didn't find the exact problem but i have re-written most of the php & now it works.
As mentioned the code i have shown works (thanks juvian); I generated this code from php & although the javascript generated was correct there was a problem with some of the php, i didn't find the exact problem but i have re-written most of the php & now it works.

Can anyone make a text(""); function?

I need help making a function that works like document.write(""); I need it to be called by using text(""); instead of printing out document.write(""); every single time. I just want to be able to use something other than document.write(""); every time. Here is what I have:
<script>
function text(a) {document.write("");}
text("Hi.");
</script>
But the function needs a parameter in the function to work. Can anyone help?
P.S. The real question is: "Can anyone help with making a text(""); function?"
Should be
<script>
function text(a) {document.write(a);}
text("Hi.");
</script>

How to pass value to using external variable in javascript?

following code works properly
draw([['Rice',20,28,38],['Paddy',31,38,55],]);
but when i try using external variable like
var val1=20;
var val2=30;
var val3=40;
draw([['Rice',val1,val2,val3],['Paddy',31,38,55],]);
It wont work.
Just showing that your example code works fine using the Firebug console. Can you post more of your code? Your stripped-down example is probably missing something else that's causing a problem.
What is your draw() function doing? Could something in that function be breaking?
EDIT: Another problem could be the trailing comma after your second array. That will throw an error in Internet Explorer.
alert([['Rice',val1,val2,val3],['Paddy',31,38,55],]);
should be:
alert([['Rice',val1,val2,val3],['Paddy',31,38,55]]);
That may solve your issue (though you also have that in your 'working' example, but I thought it worth mentioning).
Your code snippets are not equivalent -- the second one has different values (['Rice',20,30,40] vs ['Rice',20,28,38]). Other than that, they are equivalent and should have the same effects.

Categories