sradha sradha - 22 days ago 5x
jQuery Question

Literals wrapping in javascript

I am sorry .

I don't know why it is not working.

if i am writing

<button type="button" onclick="document.getElementById("demo").innerHTML = Date()">click</button>

above one is not working .

If i am writing

<button type="button" onclick="document.getElementById('demo').innerHTML = Date()">click</button>

this one is working .

the only difference i got the literals only "" ,''

But why this is occurring ?

why i have to choose the alternate literals only ,not the same?

Thank you


Because you need the complete expression

document.getElementById("demo").innerHTML = Date()

inside your onclick. If you start the onclick-expression with a double quote, the parser only knows, that another " will close the expression. That's why you have to use the different (in this case single) quote.

Same thing, when you start with a single quote:

onclick='document.getElementById('demo').innerHTML = Date()' // wouldn't work
onclick='document.getElementById("demo").innerHTML = Date()' // would work