473,749 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing selected value of options tag

HI all,
In JS, how can I change the selected value of a dropdownlist(se lect options)
I know the value of indexed value of the selected value before it is
changed.

Thanks
Robert
Jun 1 '06 #1
4 19624
Robert Bravery wrote:
HI all,
In JS, how can I change the selected value of a dropdownlist(se lect
options) I know the value of indexed value of the selected value before it
is changed.

Thanks
Robert


Hi Robert,

Just change the value of the OPTION object, like this:

<form name="myFruitsF orm" action="bla.php ">
<SELECT name="fruit" onChange="chang eValue();">
<OPTION value="apple">a pple
<OPTION value="orange"> orange
<OPTION value="pear">pe ar
</SELECT>
</form>

<script type="text/javascript">
function changeValue(){
// this function makes juice of your fruit.
var selInd = document.forms["myFruitsFo rm"].fruit.selected Index;
// get the option object
var myOption = document.forms["myFruitsFo rm"].fruit[selInd];
// and change into fruitjuice:
myOption.value = myOption.value + "juice";

// optionally change the text:
myOption.text = myOption.text + " just juiced!";
}
</script>

Regards,
Erwin Moller
Jun 1 '06 #2
HI erwin,
Thanks for this. This actually helps in a ntoher portion of my app.
But to get to this one
I think I did'nt explain myself correctly
Taking your example below
If when the webform is opened and the 'apple option is selected'
The user then makes a selection, and if they select 'orange' I want to do
something like
Alert("Sorry oranges are out of stock")
then change the selected option back to its original selection, that being
'apple'
so that when the user closes the alert box, orange is no longer selectd but
apple is

Thanks
Robert

"Erwin Moller"
<si************ *************** *************** @spamyourself.c om> wrote in
message news:44******** *************** @news.xs4all.nl ...
Robert Bravery wrote:
HI all,
In JS, how can I change the selected value of a dropdownlist(se lect
options) I know the value of indexed value of the selected value before it is changed.

Thanks
Robert


Hi Robert,

Just change the value of the OPTION object, like this:

<form name="myFruitsF orm" action="bla.php ">
<SELECT name="fruit" onChange="chang eValue();">
<OPTION value="apple">a pple
<OPTION value="orange"> orange
<OPTION value="pear">pe ar
</SELECT>
</form>

<script type="text/javascript">
function changeValue(){
// this function makes juice of your fruit.
var selInd = document.forms["myFruitsFo rm"].fruit.selected Index;
// get the option object
var myOption = document.forms["myFruitsFo rm"].fruit[selInd];
// and change into fruitjuice:
myOption.value = myOption.value + "juice";

// optionally change the text:
myOption.text = myOption.text + " just juiced!";
}
</script>

Regards,
Erwin Moller

Jun 1 '06 #3
Robert Bravery wrote:
HI erwin,
Thanks for this. This actually helps in a ntoher portion of my app.
But to get to this one
I think I did'nt explain myself correctly
Taking your example below
If when the webform is opened and the 'apple option is selected'
The user then makes a selection, and if they select 'orange' I want to do
something like
Alert("Sorry oranges are out of stock")
then change the selected option back to its original selection, that being
'apple'
so that when the user closes the alert box, orange is no longer selectd
but apple is
Hi Robert,

For that to work you'll have to store the value of the initially selected
somewhere in your script.
Then if they change the selected one, you check if it is legal.
if not, change back to old value.
(I wonder why you offer an option if it is not usefull, but that is beside
the technical point.)

And since I don't feel like real work today, try this:
<form name="myFruitsF orm" action="bla.php ">
<SELECT name="fruit" onChange="chang eValue();">
<OPTION value="apple">a pple
<OPTION value="orange"> orange
<OPTION value="pear">pe ar
<OPTION value="pineappl e">pineapple
</SELECT>
</form>

<script type="text/javascript">

var initialSelected Ind = document.forms["myFruitsFo rm"].fruit.selected Index;
var notInStore = new Array("pear","o range");

function changeValue(){
var theSelect = document.forms["myFruitsFo rm"].fruit;
var selVal = theSelect[theSelect.selec tedIndex].value;
// In store?
var inStore = true;
for(var i=0;i<notInStor e.length;i++){
if (selVal == notInStore[i]){
inStore = false;
break;
}
}
if (inStore){
// ok, set this value to remember
initialSelected Ind =
document.forms["myFruitsFo rm"].fruit.selected Index;
} else {
alert("We do not sell "+selVal+". Come back after harvest.");
// set back to original value
theSelect[initialSelected Ind].selected = true;
}

}
</script>
Make sure the first call:
var initialSelected Ind = document.forms["myFruitsFo rm"].fruit.selected Index;

is placed after the form.
If you place it above the form the browser will have trouble getting its
initial value (since the form or the selectbox don't exist). Alternatively
use unLoad() in the bodytag.

Regards,
Erwin Moller

Thanks
Robert

Jun 1 '06 #4

Hi Robert,

For that to work you'll have to store the value of the initially selected
somewhere in your script.
Then if they change the selected one, you check if it is legal.
if not, change back to old value.
(I wonder why you offer an option if it is not usefull, but that is beside
the technical point.)

Thanks for the reponse. Worked great.
For youre wonderings, this is a claim status selection, but We cannot allow
a claim status to represent closed, if there is an outstanding amount. SO I
check for closed and amount != 0 IF this is the case, we cannot use a closed
type status, so it has to warn the user, and then change back to it original
setting. However if there is no outstanding amount then allow the change
Thanks
Robert

Jun 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2121
by: John Ryan | last post by:
I've a small bit of javascript on my site that has a from with 2 selection boxes, when you choose an option in the first box, the second one re-populates its list accordingly. But the second selection box can have varying amounts of entries, and this means theres a lot of white spaces in the second selection box sometimes. Is there any way to eliminate this? Here's my code: <script language="JavaScript1.2"> <!--
7
2294
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses these lines (full text is below): if (document.form1.theDay.options.text != "Wednesday) { var days = document.form1.theDay; days.options.text = days.options.text; <snip> var option - new Option("Wednesday", 2);
3
2126
by: Fendi Baba | last post by:
I am trying to write a simple Javascript code to pass value from a listbox to another field the following is my code f=document.forms; window.alert(f.FieldList.options.length) for(i=0;i<f.FieldList.options.length;i++) { if(f.FieldList.options.selected){
2
8094
by: Monty | last post by:
I use a SELECT dropdown as the nav interface for jumping to a chosen page number. When I setup up the SELECT element on the page, I want to show the user the current page number they are on, so, I put "selected" next to the page number, as shown below: ------------------------------- <form name="pick" method="post" action="showit.php"> <select name="pggo" onChange="this.options.selected;document.pick.submit();">
3
8718
by: imrantbd | last post by:
I need array type name like "destList" must use for my destlist select box,not a single name.Or need a solution to capture multiple value of "destList" select box and send all selected value in php page.The multiple select value then insert in database added by comma.The following is my code: Form Page:form.php <head> <script language="JavaScript"> function addSrcToDestList() { destList1 = window.document.forms.destList; srcList...
4
1748
by: shankwheat | last post by:
I'm passing a string like 'Div3,Div4' to use as options in a selectbox but I need the text the user sees in the box different from the values. My code populates the values in the selectbox correctly but not the text. Thanks Desired end result: <select id="selectBox" multiple="multiple" size="4"> <option value="Div3">About the Company</option> <option value="Div1">Ratings</option>
2
2020
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name of the group that will receive the e-mail. I pass this.form to a function to validate the other data before handing it off to a script to actually do the mailing. The mailer script looks at all of the form's fields and sends their names and...
1
7320
by: vraamu | last post by:
here in this option transfer code.after transfering data from available _services multiple box to assigned_services multiple box i am unable to get the value of the assigned_services data for ex in this <option value ="0" >Admin</option> <option value ="1" >Public</option> i am not getting 0 and 1 but i am getting admin,public {php} include "header2.php"; {/php} {literal} <link rel="stylesheet" type="text/css" href="css/styles.css"...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.