468,121 Members | 1,473 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

can I use confirm to stop a select list change?

Hi

I want to warn the user that changing the selected item in a pull-down
will clear some data he has already entered in a file download box.

I've tried using confirm() to return true or false with a line such as:

onchange="return(confirm('proceed Yes or No')))"
but it always stops the change.

Is what I'm trying to do possible?

John South
Pangbourne UK

Jul 23 '05 #1
4 19134
Jo**********@gmail.com wrote:
Hi

I want to warn the user that changing the selected item in a pull-down
will clear some data he has already entered in a file download box.

I've tried using confirm() to return true or false with a line such as:

onchange="return(confirm('proceed Yes or No')))"
There's one to many closing brackets...------^
But maybe that's just a posting typo.
but it always stops the change.
Stops what change?

You can't use onchange to prevent a user changing a selection - it
runs after they've made the change (usually when the element loses
focus, but not always).

Not sure what you mean by "a file download box" - you can't change
the value of a file input ( <input type="file" ...> ) other than to
clear it with reset if it's in a form.

Is what I'm trying to do possible?


So far, no, but maybe. Supply a bit more code or fuller explanation
to illustrate what you are trying to do.

A confirm gives the user an opportunity to conditionally execute some
script, I can't see that it has any use as the sole action of an
onchnage event.

Here's a bit of play code:

<form name="formA" action="">
<select name="selA" onchange="
if (confirm('Proceed Yes or No')){
selIdx = this.selectedIndex;
// Do whatever if user says OK
// ...
} else {
// Don't do it and return selection to previous
this.selectedIndex = selIdx;
}
">
<option selected>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>
</form>
<script type="text/javascript">
// Set selIdx to defalut selected index on page load
var selIdx = document.formA.selA.selectedIndex;
</script>
--
Rob
Jul 23 '05 #2
Rob,
Thanks for the clear and full reply. I think the answer to my question
is no, but I'll explain a bit more detail just in case you see an
opening:

On my form I have an input type="file" upload control and a number of
text boxes. I also have a select control to select the JobType. The
number of text boxes depends on the JobType. When the user selects a
JobType I do an "AutoPostBack" (asp.net c#) to refresh the page with
the correct text boxes. Unfortunately if the user has already browsed
for a file to upload, this is cleared with the refresh.

If the user changes the JobType I'd like to check if the file upload
control contains a value and, if so, warn the user that the control
will be cleared. I'd like to give him the chance to cancel and to
return the JobType to it's value before the last change, and not do the
auto call back.
Am I asking too much?

John South

Jul 23 '05 #3
ASM
Jo**********@mail.com wrote:
Rob,
Thanks for the clear and full reply. I think the answer to my question
is no, but I'll explain a bit more detail just in case you see an
opening:

On my form I have an input type="file" upload control and a number of
text boxes. I also have a select control to select the JobType. The
number of text boxes depends on the JobType. When the user selects a
JobType I do an "AutoPostBack" (asp.net c#) to refresh the page with
the correct text boxes.
I do not understand Why you have to refresh the page
only to change some fields.
Unfortunately if the user has already browsed
for a file to upload, this is cleared with the refresh.
Except if your php send back the right values
If the user changes the JobType I'd like to check if the file upload
control contains a value and, if so, warn the user that the control
will be cleared. I'd like to give him the chance to cancel and to
return the JobType to it's value before the last change, and not do the
auto call back.
Am I asking too much?


No, because I don't understand what exactly do the
onchange on select if I'd confirm.
Is this select a submit button ?

<select onchange="var n=this.options.selectedIndex;
var o = this.options[n].value
if(confirm('You\'re sure of your choice?\nif OK page will change'))
{ document.myForm.ind.value = o;
document.myForm.submit(); }
else
{ alert('No change done');
this[0].selected=true;
document.myForm.ind.value = '';}">

document.myForm.ind is a hidden field initially empty
(to tell to php what to do)

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #4
Jo**********@gmail.com wrote:
Rob,
Thanks for the clear and full reply. I think the answer to my question
is no, but I'll explain a bit more detail just in case you see an
opening:

On my form I have an input type="file" upload control and a number of
text boxes. I also have a select control to select the JobType. The
number of text boxes depends on the JobType. When the user selects a
JobType I do an "AutoPostBack" (asp.net c#) to refresh the page with
the correct text boxes. Unfortunately if the user has already browsed
for a file to upload, this is cleared with the refresh.

If the user changes the JobType I'd like to check if the file upload
control contains a value and, if so, warn the user that the control
will be cleared. I'd like to give him the chance to cancel and to
return the JobType to it's value before the last change, and not do the
auto call back.
Am I asking too much?


I'm not familiar with ASP.NET or C#, I imagine that an onchange
function is being created for the client-side page that either posts
back the form or uses XMLHTTPrequest to get new content for the page.

Either way, you need to post whatever is being delivered to the client.
The code I posted above conditionally changes a select back to the
previous selection. You need to insert something like that into the
onclick generated by the ASP page (if there is one) or code it
yourself.

Although you can't change the file input's value, you can read it. So
test if it's empty and respond accordingly:
<form name="formA" action="">
<input type="file" name="xx"><br>
<select name="selA" onchange="
if ( '' != this.form.xx.value )
if ( ! confirm('Proceed Yes or No')){
// Return selection to previous & don't proceed
this.selectedIndex = selIdx;
return;
}
}
// Remember selection, do stuff 'cos user said OK
// or hasn't selected a file yet
selIdx = this.selectedIndex;
// ...
}
">
<option selected>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>
</form>
<script type="text/javascript">
// Set selIdx to default selected index on page load
var selIdx = document.formA.selA.selectedIndex;
</script>
An alternative is to put all of your fields in the form, then hide or
show groups of them depending on which selection the user has made.
Then they don't loose anything that they've already filled in.

This approach should display all the fields by default and use script
to hide them. Then if a user doesn't have scripting enabled, they can
still use the form (autpostback depends on client-side scripting).
--
Rob
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by zsh-users-help | last post: by
1 post views Thread by SimoRed | last post: by
18 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.