Reputation: 101
I am having trouble on passing time interval to javascript. The time interval is formatted using minus sign (Eg. 10-11 means from 10 to 11 AM/PM doesn't matter).
In markup I have something like this.
<? $mytime = '10-11';?>
<a href="" onclick="checkTime($mytime)"></a>
In Javascript I have this
function checkTime(mytime) {
console.log(mytime);
}
Console window show -1. i need to have the same string I passed. What should I do.
Upvotes: 0
Views: 40
Reputation: 461
You are printing your php string directly in the onlick function, this will output something like this:
<a href="" onclick="checkTime(10-11)"></a>
You can easily see how this will not work. That's why you need to put the single inverted commas before printing your php var, so that it appears as a string in js: onlick="checkTime('10-11')"
Upvotes: 0
Reputation: 25352
Pass value like this
<a href="#" onclick="checkTime('$mytime')"></a>
instad of
<a href="" onclick="checkTime($mytime)"></a>
You are passing 10-11
. which without '' javascript consider this as number.
This make 10-11=-1
Something like this
function v(i){console.log(typeof i);}
v(10-11) // number
v("10-11") // string
Upvotes: 1