+ 1

Where we must use single quote in method? e.g. alert('single')

<input type="button" onclick="alert("double")"> doesn't work but why using this - alert('single') work? Is there any specific rule?

4th May 2017, 2:39 PM
Pintu
Pintu - avatar
6 Respostas
+ 17
@Apel i know, i used the word "Generally" for this... because is not possible in all cases escape double quote... maybe i don't explained good, i wrote it only for completeness. =^=
4th May 2017, 9:33 PM
Maz
Maz - avatar
+ 16
Yes, in short: If you open a statement with a double quote, you have to close that statement with the same double quote. "Hey...this is 'Kaneki'" // It's Okay! "Hey...this is "Kaneki"" // It's not Okay! Generally... you can use the backslash for "skip the problem" "Hey... this is \"Kaneki\"" // It's Okay, now!
4th May 2017, 2:44 PM
Maz
Maz - avatar
+ 5
You are using onclick HTML attribute here and attribute value is placed inside double quote. You can't use double quote inside other double quotes without escape them with back slash (\). But you can escape anything inside HTML attribute. So you have to use single quote here. TIPS: Don't handle event inside HTML (eg, onclick, onmouseover, onmouseleave etc), rather you can add an event listener in Javascript to that DOM element.
4th May 2017, 2:52 PM
Apel Mahmod
Apel Mahmod - avatar
+ 4
@Maz your answer is correct for javascript where we have to escape characters, but he was using alert for html attribute. you cant escape double quote as html attribute value
4th May 2017, 9:22 PM
Apel Mahmod
Apel Mahmod - avatar
+ 2
if you use double quotes throughout, it confuses the interpreter which will think you are trying to say ="alert(" and then Double"("> which doesn't make sense. So the single quotes help the interpreter understand where each statement starts and ends.
4th May 2017, 2:46 PM
shaun khulani
shaun khulani - avatar
+ 1
Thanks.. now i got 3 different properties where and how i should use ' or " ...
4th May 2017, 8:32 PM
Pintu
Pintu - avatar