Adding javascript Placeholders inside PHP code?

Hi,I’ve been trying to add some placeholders to some fields without any luck. Here is the placeholder: I just can’t figure out how to dots and apostrophes go. I’m curious how it will work. Thanks

 onfocus="this.placeholder=''" onblur="this.placeholder='<?php echo $this->__('28') ?>'" placeholder="<?php echo $this->__('28') ?>"

<input type="text" id="' . $this->getFieldId('day') . '" name="' . $this->getFieldName('day') . '" value="' . $this->getDay() . '" title="' . $this->__('Day') . '" class="input-text validate-custom" ' . $this->getFieldParams() . ' />

you need to escape the quotes (that’s called ‘regarding the context change’). in PHP you’d use htmlspecialchars() for that.

note that there is no need to add/delete the placeholders dynamically. they usually hide on focus anyways.

I don’t know everything I’ve tried doesn’t work. Here’s how I tried with htmlspecialchars.I must have done it wrong.

<input type="text" id="' . $this->getFieldId('day') . '" name="' . $this->getFieldName('day') . '" value="' . $this->getDay() . '" title="' . $this->__('Day') . '" class="input-text validate-custom" ' . $this->getFieldParams() . ' onfocus=&quot;this.placeholder=''&quot; onblur=&quot;this.placeholder='. $this->__('28').'&quot; placeholder=&quot;'.$this->__('28').'&quot; />

that’s correct. you must not escape the surrounding quotes (because they define the attribute value) but the surrounded quotes (the quotes that are part of the value itself).

Okay, here it is before I do anything to it:

onfocus="this.placeholder=''" onblur="this.placeholder='<?php echo $this->__('28') ?>'" placeholder="<?php echo $this->__('28') ?>"

Here it is after removing the php statements:

onfocus="this.placeholder=''" onblur="this.placeholder='.$this->__('28').'" placeholder="'.$this->__('28').'"

I don’t see any place where the quotes are part of the value?

but isn’t that what you said you have problems with?

Well yeah. I’m having a problem mostly because there is javascript inside not just php.

drop the placeholder rewriting. placeholders are automatically removed when a field gets focus.

I like it with the javascript because right when you click into the field the placeholders disappear. Regular placeholders don’t disappear until one begins to type. Not that big a deal I know. But I have the javascript kind on all my forms and was trying to stay consistent. But if I must I’ll will use regular placeholders. : )

To me your solution is a step back in usability - why would I want the placeholder to disappear before I begin to type? The placeholder is supposed to provide me some useful information, it’s not convenient to have to focus away from a field just to see the placeholder. Sometimes I may forget what the placeholder was before moving focus to the field.

And sometimes a field gets focused automatically. So you don’t even had the chance to see that there is a placeholder at all.

1 Like

Yes, I understand what ya’ll are saying. I’m not putting anything my placeholders one would have to remember. Under the fields I’ve added additional instructions. For example, in the placeholder for street I have something like 123 Somestreet Dr. Under that I give additional instructions in grayed text.

No big deal they work find.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.