0

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.

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Explaining why you surround the variable with single quotes will be more helpful
Thank you so much , It worked and I understood how it works now. @Anik Islam Abhi
Now this explains a lot :)
1

try:

<a href="" onclick="checkTime('<? echo $mytime ?>')"></a>

Comments

0

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')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.