Search code examples
javascriptonclickdom-eventsonblur

Onclick works in isolation but not on web page


On a test page this works find with static variables:

<input class="txtSearch"
       onblur="if(this.value == '')this.value='Search' ;"
       onfocus="if(this.value == 'Search') this.value='';"
       value="Search" name="s" type="text" size="15" />

When I place it inside my page switching the static variables with dynamic PHP ones I get a white page. Am I escaping them correctly inside of a function?

function wpsb_show_form($rtn = 0) { 
    $wpsb_flds = (get_option('wpsb_form_fields'));
    $add_link_lv = get_option("wpsb_link_love");
    $out = '<form action="#wpsbw" method="post">' . "\n";

    if (is_array($wpsb_flds)) {
        foreach ($wpsb_flds as $wpsb_k => $wpsb_v) {
            if (is_numeric($wpsb_k) && $wpsb_v) {
                $out .= '<ul class="optin">';
                $out .= '<li><span>Subscribe to our Newsletter</span></li>';
                $out .= '<li><input type="text" name="wpsb_fld['. $wpsb_k .']" id="wpsb_fld_'. $wpsb_k .'"  maxlength="64" class="txt" onblur="if(this.value == '') this.value="'. $wpsb_k .'" ;" onfocus="if(this.value == "'. $wpsb_k .'") this.value='';" value="'. $wpsb_k .'" /></label></li>' . "\n";
        }
    }
}

Do I need to escape the Javascript conditional statements?


Solution

  • You need to escape your single quotes (') in the line where you output the javascript.

    Remember:

    print '\''; # output: '
    print '"';  # output: "
    print "'";  # output: '
    print "\""; # output: "