Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So one thing I often do in the codebehind is setup the javascript events to avoid in-line code.

C#
myButton.Attributes.Add("onclick", string.Format("fnDoSomething(this, '{0}', '{1}');", myList.CliendId, myStatus.CliendId));


Very standard, nothing exciting. But one day I forgot the quotes:

C#
myButton.Attributes.Add("onclick", string.Format("fnDoSomething(this, {0}, {1});", myList.CliendId, myStatus.CliendId));


To my surprise in the script I had actually passed the myList and myStatus objects themselves.... not the IDs.

This seemed pretty cool, no need to do a document.getElementById. But I was worried this wouldn't work on all browsers or in all circumstances.

Can anyone say difinitively if this is bad practice?
Posted
Updated 8-Aug-11 0:29am
v2

1 solution

Nothing is surprise here. If you use single quotes, Javascript accept that as a string. So, you have to use document.getElementById to get the actual id. Whereas, if you are not using single quotes, Javascript will directly take this as a object. So, you don't want to use document.getElementById again.
 
Share this answer
 
Comments
Chris_Green 8-Aug-11 6:39am    
That's not what I was asking.

This is the question: But I was worried this wouldn't work on all browsers or in all circumstances. Can anyone say difinitively if this is bad practice?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900