Welcome, Guest

Writing a dynamic prompt script
(1 viewing) (1) Guest
  • Page:
  • 1
  • 2

TOPIC: Writing a dynamic prompt script

Writing a dynamic prompt script 3 years, 2 months ago #3211

Once I get this working, I intend to package it and post it for you guys. But I need a little help.

I'm trying to write a small script to have form prompts, like the more professional sites use. An example is:

(imagine this line in a form field)

Your Name_____

Click on the box, and it just shows:

______________

So it deletes the input value to make room for what you type. Most importantly, it must not delete what the user puts in there if they blur then re-click on the field.

Also, I tried to make it so that each element stores its initial value as a global property, instead of making it a variable in the function; so that each individual form field would remember its individual prompt.

The script I've written looks like this:

function irs_prompt(element,action) {
if (element.init_value_set==="") {
element.init_value_set=1;
if (element.init_value==="") {
element.init_value=element.value;
}
}
if (action == "click") {
if (element.value == element.init_value) {
element.value = "";
}
}
if (action == "blur") {
if (element.value === "") {
element.value = element.init_value;
}
}
}


And it does nothing at all. I've been scratching my head over this; I've got the parameters of the form object set correctly, i.e. function name equals script name, and the 'click' and 'blur' checkboxes are checked (I'm using the sample contact form name field to test, and I've removed the 'get focus' initial script) so I have to be failing in my javascript somehow. However, I'm no scripting guru, so I just can't see what I'm doing wrong right now. Does anybody else see what's failing here?
Last Edit: 3 years, 2 months ago by CircularLogician.

Re:Writing a dynamic prompt script 3 years, 1 month ago #3284

first of all there can't be 3x '='

if (element.value === "")


compare operator is '==' so

if (element.value == "")


Check it if this is not mistake made by this forum script.
  • b0rek
  • OFFLINE
  • Junior Boarder
  • Posts: 20
  • Karma: 1
Last Edit: 3 years, 1 month ago by b0rek.

Re:Writing a dynamic prompt script 3 years, 1 month ago #3291

For starters add ActionScript to your inputbox (named foo) for Blur and Focus

function ff_fk_foo_action(element, action)
{
    switch (action) {
        case 'blur':
            if(element.value=='') element.value='Your prompt';
            break;
        case 'focus':
            if(element.value=='Your prompt') element.value='';
            break;
        default:;
    } // switch
} // ff_fk_name_action



This works nicely. Maybe it is worth consider to parametrize it so it could be added to Script Library?
  • b0rek
  • OFFLINE
  • Junior Boarder
  • Posts: 20
  • Karma: 1

Re:Writing a dynamic prompt script 3 years, 1 month ago #3381

Yeah, parameterizing it was the goal. In fact, what I'm trying to do is get it to read the initial value of the textfield and keep that in a stored variable; otherwise, I have to write the same script with a different prompt for every field in the form, and that can bloat the code immensely.

The script I wrote didn't work with '==' or '==='; I used Jlint on it, and it had suggested ===, so that's how I ended up using it. I agree, == is probably the right way to do it.
  • CircularLogician

Re:Writing a dynamic prompt script 3 years, 1 month ago #3382

Er, Actionscript? As in flash?

Re:Writing a dynamic prompt script 3 years, 1 month ago #3383

Are you kidding, right?

Action Script - in Breezing Forms, script connected to action on form element. Actions: Click, Blur, Change, Focus, Select.
  • b0rek
  • OFFLINE
  • Junior Boarder
  • Posts: 20
  • Karma: 1
  • Page:
  • 1
  • 2
Moderators: TheMuffinMan, ForumSupport
Time to create page: 0.37 seconds

About

Crosstec GmbH & Co. KG

Bergisch-Gladbacher-Str. 829

51069 Cologne, Germany