$form = '<form role="search" method="POST" id="searchform" action="' . home_url( '/' ) . '" >
<input type="text" name="s" id="s" value="search..." onfocus="if(this.value==this.defaultValue)this.value="";" onblur="if(this.value=='')this.value=this.defaultValue;" />
<button type="submit" id="searcbutton" value="go" ></button>
</form>';
I found this error -
"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING"
oikram
August 12, 2010, 11:53am
2
Yes. By using “” instead of ‘’ We don’t get the syntax error, but if we use “” on the onBlur we will have problems of course. :s My bad.
Oh well… I thought that I got my first answer to someone else.
I will keep on trying.
Won’t that affect the full value of the onblur attribute?
What will work is to escape the single quotes, so that they are \’ instead
onblur="if(this.value==\\'\\')this.value=this.defaultValue;"
That way it’ll look like this on the web page
onblur="if(this.value=='')this.value=this.defaultValue;"
And of course, the best solution is to not embed your javascript code within your HTML code at all. Instead, use progressive enhancement techniques where the javascript is placed at the end of the body, where it can attach the events on to the page elements as needed.
onblur="if(this.value=="")
That won’t work either, as the browser will think the onblur attribute is closed by the first " it finds.
Try this:
$form = '<form role="search" method="POST" id="searchform" action="' . home_url( '/' ) . '" >
<input type="text" name="s" id="s" value="search..." onfocus="if(this.value==this.defaultValue)this.value=\\'\\';" onblur="if(this.value==\\'\\')this.value=this.defaultValue;" />
<button type="submit" id="searcbutton" value="go" ></button>
</form>';
I’ve escaped the apostrophes in there:
onblur="if(this.value==\\'\\')this.value=this.defaultValue;"
oikram
August 12, 2010, 11:29am
5
$form = '<form role="search" method="POST" id="searchform" action="' . home_url( '/' ) . '" >
<input type="text" name="s" id="s" value="search..." onfocus="if(this.value==this.defaultValue)this.value="";" onblur="if(this.value=='')this.value=this.defaultValue;" />
<button type="submit" id="searcbutton" value="go" ></button>
</form>';
Change to:
$form = '<form role="search" method="POST" id="searchform" action="' . home_url( '/' ) . '" >
<input type="text" name="s" id="s" value="search..." onfocus="if(this.value==this.defaultValue)this.value="";" onblur="if(this.value=="")this.value=this.defaultValue;" />
<button type="submit" id="searcbutton" value="go" ></button>
</form>';
Does it works?
On detail:
You had:
onblur="if(this.value=='')
And the single quotes where ending string encapsulation.
Changed to:
onblur="if(this.value=="")
Regards,
Márcio