+ 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?
6 ответов
+ 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. =^=
+ 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!
+ 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.
+ 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
+ 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.
+ 1
Thanks.. now i got 3 different properties where and how i should use ' or " ...