Search code examples
phponbluronfocus

Onblur/onfocus not working inside php


I'm trying to add onfocus/onblur events to a form, but somehow I'm not getting it to work. I think it has to do with escaping the quotes, but I've tried several ways with no success. My code is this:

    echo '<input type="password" id="'.$field->name.'" name="'.$field->name.'" size="30" class="inputbox" placeholder="'.$field->title.'"
                onfocus="this.placeholder = ''" onblur="this.placeholder = '.$field->title.'"
                />'."\n";
                break;

Solution

  • You're missing quotes around your onblur string:

    <?php
    echo '<input type="password" id="' . $field->name . '" name="' . $field->name . '" size="30" class="inputbox" placeholder="' . $field->title . '" onfocus="this.placeholder = ''" onblur="this.placeholder = \'' . $field->title . '\'" />'."\n";