View Full Version : Syntax Question: What are the +'s for?

04-14-2007, 09:54 PM
I have been using this code for years to allow a user to enter a number of hours (varialbe Hr) and minutes (Mn). Every few months a single user will report that the <input> box for minutes contains:


and they can't get rid of it. In looking at the coding I am wondering if instead of value="+Mn+" I should use single quotes: value='+Mn+'

Perhaps their browser is getting confused by the double quotes within double quotes?

But it's been so long since I coded it I no longer remember what the plus signs on either side of the variable Mn are for. I want to understand the code again before I go changing things.

c+="<input type=text name=hr value="+Hr+" onChange='parent.create.setHr(this)' ";
c+="size=2 maxlength=2 class='textBox' border=1>Hour";
c+="<input type=text name=mn value="+Mn+" onChange='parent.create.setMn(this)' ";
c+="size=2 maxlength=2 class='textBox' border=1>Minutes";

I'd also appreciate your looking over the code.

Thanks, Peter

04-14-2007, 11:06 PM
Because you didn't enclose the attribute values in double quotes and if Mn is empty, the value of the textbox becomes the onChange handler. This would be the output:

<input type=text name=mn value= onChange='parent.create.setMn(this)' size=2 maxlength=2 class='textBox' border=1>Minutes
You should always put double quotes in attribute values. Use single quotes in Javascript string literal to make it less confusing.

c+='<input type="text" name="hr" value="'+Hr+'" onchange="parent.create.setHr(this)" ';
c+='size="2" maxlength="2" class="textBox" border="1">Hour';
c+='<input type="text" name="mn" value="'+Mn+"' onchange="parent.create.setMn(this)" ';
c+='size="2" maxlength="2" class="textBox" border="1">Minutes';

04-14-2007, 11:30 PM
It always seemed to work with out the quotes (I guess it wasn't!) so I figured I could just leave them out.

Thanks, Peter