The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Javascript to clear a form field issue
Lexx87
post Apr 5 2011, 12:57 PM
Post #1





Group: Members
Posts: 3
Joined: 4-April 11
Member No.: 14,265



CODE

else if ( document.flight_form.destination.value != "Paris" )
{

document.flight_form.destination.value += "Paris";
document.flight_form.departfrom.focus(); //as the field is filled for the user, the next field is selected for them
document.getElementById("error").innerHTML="We're sorry, due to problems beyond our control only flights to Paris are currently availiable.";
valid = false;    
}

That code, if the value of the text box 'destination' is not Paris, it puts Paris in the text box. Yet the way that works, if say "Spain" is in the text box, all it will do is append Paris to it so the box will have "SpainParis" inside it.

I want to remove any string in the box and then insert Paris in its place.

I've tried "document.flight_form.destination.reset(); the line before, but that either doesn't work as the interpreter doesn't process line by line (not sure how it works), or reset() is not the right thing to use..or...well that's where i'm stuck!

Any help would be amazing.

Alex

This post has been edited by Lexx87: Apr 5 2011, 01:02 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post Apr 6 2011, 02:12 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



Change this:
CODE
document.flight_form.destination.value += "Paris";

to this:
CODE
document.flight_form.destination.value = "Paris";
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Apr 6 2011, 02:55 AM
Post #3


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



QUOTE(Frederiek @ Apr 6 2011, 04:12 PM) *

Change this:
CODE
document.flight_form.destination.value += "Paris";

to this:
CODE
document.flight_form.destination.value = "Paris";




It seems to me this is not a helpful answer. Of course it helps the questioner, who mindlessly pastes your correction in, and disappears until the next question, but it does not _help_ in the long term unless you at least explain *why* the correction works.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 6 2011, 10:25 AM
Post #4


.
********

Group: WDG Moderators
Posts: 7,858
Joined: 10-August 06
Member No.: 7



QUOTE(Lexx87 @ Apr 5 2011, 07:57 PM) *

That code, if the value of the text box 'destination' is not Paris, it puts Paris in the text box.

Wouldn't it be more user-friendly to say "Only flight to Paris are available" on the page right from the start? You might add a READONLY attribute to the text field to prevent users from changing it.

QUOTE(Brian Chandler @ Apr 6 2011, 09:55 AM) *

It seems to me this is not a helpful answer. Of course it helps the questioner, who mindlessly pastes your correction in, and disappears until the next question, but it does not _help_ in the long term unless you at least explain *why* the correction works.

So why does it work? wink.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Apr 6 2011, 10:48 AM
Post #5


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



I think you know what I mean. If people come here to be spoonfed with "Replace [string of gobbledygook] with [different string of gobbledygook] " they are not going to learn anything.

Does the OP understand what the javascript += operator means? Presumably not: it means "append". So if x is the string 'plip", then x += '-plop' appends -plop, giving x the value 'plip-plop'.

But as you say (and your answer seems to me vastly more constructive), this is all a barmy way to be doing things in the first place.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 22nd July 2018 - 03:37 PM