re-select value in a combobox

B

Bob Bedford

I've a form that use a combobox along with other fields. When the user
submit the form, many tests are done. If any test fails, then I show the
form again with previously entered values.

My problem: when I show back the form with previously entered values, I
can't set the entered value of the combobox. I've registered the value, but
how to go trough the value, and select the right one.

I'd like something like:

for each record in combobox
if record.value = savedvalue then
record.select
break;
end

It is possible with javascript. Please take in mind for your answer that
I'ven't worked so far with javascript.

my form's name is formSubmit
my combobox's name is City

Thanks for help.

BoB
 
R

Richard Cornford

Bob Bedford wrote:
My problem: when I show back the form with previously
entered values, I can't set the entered value of the
combobox. I've registered the value, but how to go
trough the value, and select the right one.

I'd like something like:

for each record in combobox
if record.value = savedvalue then
record.select
break;
end

It is possible with javascript. Please take in mind for
your answer that I'ven't worked so far with javascript.
<snip>

Bad plan, very unreliable. Get the server-side process that writes the
option elements into the HTML to add a - selected - attribute to the one
you want pre-selected and it will be pre-selected. There is no need to
make this process dependent on client-side scripting at all.

Richard.
 
B

Bob Bedford

Thanks for your reply, Richard,

As I told, I'm not so confortable with JS. Here is the function I've been
able to write, but don't know where to add the "selected" attribute. The
function is called by a new window that has PHP code for reading the MySQL
datas:

function AddItem(ItemIndex,ItemName) {
//alert(ItemName);
destinationList = document.getElementById('City');
var newOpt = new Option(ItemName,ItemIndex);
destinationList.options[destinationList.length] = newOpt;
}

So for every new record in the query, I add a new value in the combobox. How
to add a "selected" attribute in this code ?

Thanks for helping.

BoB
 
M

Mick White

Bob said:
Thanks for your reply, Richard,

As I told, I'm not so confortable with JS. Here is the function I've been
able to write, but don't know where to add the "selected" attribute. The
function is called by a new window that has PHP code for reading the MySQL
datas:

function AddItem(ItemIndex,ItemName) {
//alert(ItemName);
destinationList = document.getElementById('City');
var newOpt = new Option(ItemName,ItemIndex);
destinationList.options[destinationList.length] = newOpt;
}

So for every new record in the query, I add a new value in the combobox. How
to add a "selected" attribute in this code ?

destinationList.selectedIndex=destinationList.length-1;
or
with(destinationList) {selectedIndex=length-1}

Mick
 
B

Bob Bedford

destinationList.selectedIndex=destinationList.length-1;
or
with(destinationList) {selectedIndex=length-1}

It's not exactly what I want. Takes the code:
function AddItem(ItemIndex,ItemName) {
destinationList = document.getElementById('City');
var newOpt = new Option(ItemName,ItemIndex);
destinationList.options[destinationList.length] = newOpt;
}

takes the example:
AddItem(412,'Las Vegas');
AddItem(312,'New York);
AddItem(174,'Baltimore');
AddItem(588,'Washington');

Now, How to put "selected" attribute to the Item wich ItemIndex = 174 ?

174 is the value selected by the user, so I must put selected to the item
once it's created.

Also, how to get the "selected" itemIndex (I mean the value 174) when I've
clicked the combobox ?

BoB
 
M

Mick White

Bob said:
destinationList.selectedIndex=destinationList.length-1;
or
with(destinationList) {selectedIndex=length-1}


It's not exactly what I want. Takes the code:
function AddItem(ItemIndex,ItemName) {
destinationList = document.getElementById('City');
var newOpt = new Option(ItemName,ItemIndex);
destinationList.options[destinationList.length] = newOpt;
}

takes the example:
AddItem(412,'Las Vegas');
AddItem(312,'New York);
AddItem(174,'Baltimore');
AddItem(588,'Washington');

Now, How to put "selected" attribute to the Item wich ItemIndex = 174 ?



174 is the value selected by the user, so I must put selected to the item
once it's created.

If the user selects 174, it is "selected". Do you want javascript to
remember which option was selected?
Also, how to get the "selected" itemIndex (I mean the value 174) when I've
clicked the combobox ?

Your server side script will be able to determine which option is selected.

var x=this.options[this.selectedIndex]
var areaCode=x.text
var city=x.value

where "this" refers to the select Object"
<select onchange="var x=this.options[this.selectedIndex];alert('value:
'+x.value+'\ntext: '+x.text+'\nindex:'+this.selectedIndex) "
Mick
 
T

Thomas 'PointedEars' Lahn

Bob said:
function AddItem(ItemIndex,ItemName) {

You should avoid using function identifiers starting with capital
letters if the function is not going to be used as a constructor.
//alert(ItemName);
destinationList = document.getElementById('City');
^^^^^^^^^^^^^^
I do hope you test for that DOM property prior to access,
see <http://pointedears.de/scripts/test/whatami>.

if (destinationList)
{
var newOpt = new Option(ItemName,ItemIndex);

if (newOpt)
{
newOpt.selected = "selected"; // should be XHTML compatible
destinationList.options[destinationList.length] = newOpt; }
}
}

If that does not work, try

A) newOpt.selected = true;

B) var newOpt = new Option(ItemName, ItemIndex, true);
So for every new record in the query, I add a new value in the combobox.
How to add a "selected" attribute in this code ?

See above and please RTFineM next time:

<http://devedge.netscape.com/central/javascript/>
Thanks for helping.

You're welcome.
[Top post]

See the FAQ.


PointedEars
 
R

Randy Webb

Thomas said:
Bob Bedford wrote:




You should avoid using function identifiers starting with capital
letters if the function is not going to be used as a constructor.

Do you have some off the wall reason for that advice? Javascript doesn't
care what you name functions, as long as it doesn't interfere/clash with
reserved names and names used elsewhere. Which typically makes
CamelNames *safer*.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 8 Aug
2004 20:07:26, seen in Thomas 'superfluous
information' Lahn said:
[Top post]

See the FAQ.

Inadequate citation. You should either give the URL, or specify it as
being regularly posted in the newsgroup. One who does not comply with
the FAQ should not be presumed to have the intelligence to find it
unaided.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 8 Aug
2004 14:42:44, seen in Randy Webb
Do you have some off the wall reason for that advice? Javascript doesn't
care what you name functions, as long as it doesn't interfere/clash with
reserved names and names used elsewhere. Which typically makes
CamelNames *safer*.

Including an underline character may also help.

I once used a system in which the set of identifiers beginning with "Q"
followed by a letter other than "U" was reserved to the system authors.
A programmer could this readily avoid any chance of a clash, and in
reading old code could immediately recognise which were library
routines.


I just came across an amusing clash.

In my vb-dates.htm, there are a number of section headers such as
<H3><a name="WD">Weekday Arithmetic</a></H3>

Today, I noticed that that one, unlike the rest, was no longer black as
it had undoubtedly been when first created, but was coloured and
decorated as for a link - and behaved as a link to a non-existent page.

It turned out that in more recent code a variable WD was introduced,
which was not declared as local, and was given a value compatible with
the name of the non-existent page.

Changing the anchor to WDA fixed the matter; the variable is now
properly localised.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top