I have a form built with jquery. I just want to have an empty form with onfocus set so when a user types something the text disappears.
var $myinput2 = js("<input id='CameraName' name='camera_name' size='24' maxlength='36' value='Enter label for camera' onfocus='if(this.value=='Enter label for camera') this.value='';'/>");
I get a syntax error pointing at the “if(this.value==”
I can’t seem to get the quoting right so this will work. Thanks in advance.
You just have to be careful about escaping quotation marks within a string literal if they’re the same type as the whole string literal is enclosed by.
You’re using single-quotes for your html attributes, so within the inline
onfocushandler your JS should use double-quotes but because the whole string is in double-quotes you have to escape those double-quotes:That is, within your string literal the inline
onfocuswill look like this:Such that the actual value is:
Note that you wouldn’t have this problem if you didn’t use an inline event handler and instead did something like:
(I’m assuming that
jsis your alias forjQuery.)