473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

re-select value in a combobox

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
Jul 23 '05 #1
9 5075
Bob Bedford wrote:
<snip>
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.
Jul 23 '05 #2
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

"Richard Cornford" <Ri*****@litotes.demon.co.uk> a écrit dans le message de
news:ce*******************@news.demon.co.uk...
Bob Bedford wrote:
<snip>
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.

Jul 23 '05 #3
Bob Bedford wrote:
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.leng th-1;
or
with(destinationList) {selectedIndex=length-1}

Mick
Thanks for helping.

BoB

"Richard Cornford" <Ri*****@litotes.demon.co.uk> a écrit dans le message de
news:ce*******************@news.demon.co.uk...
Bob Bedford wrote:
<snip>
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.


Jul 23 '05 #4
> destinationList.selectedIndex=destinationList.leng th-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
Jul 23 '05 #5
Bob Bedford wrote:
destinationList.selectedIndex=destinationList.le ngth-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
BoB

Jul 23 '05 #6
Bob Bedford wrote:
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/>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/option.html>
Thanks for helping.
You're welcome.
[Top post]


See the FAQ.
PointedEars
Jul 23 '05 #7
Thomas 'PointedEars' Lahn wrote:
Bob Bedford wrote:

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.


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*.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8
JRS: In article <12****************@PointedEars.de>, dated Sun, 8 Aug
2004 20:07:26, seen in news:comp.lang.javascript, Thomas 'superfluous
information' Lahn <Po*********@web.de> posted :
[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.

--
© John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
Dates - miscdate.htm Year 2000 - date2000.htm Critical Dates - critdate.htm
Euro computing - eurocash.htm UK Y2k mini-FAQ: y2k_mfaq.txt Don't Mail News
Jul 23 '05 #9
JRS: In article <H8********************@comcast.com>, dated Sun, 8 Aug
2004 14:42:44, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Thomas 'PointedEars' Lahn wrote:
Bob Bedford wrote:

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.


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #10

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

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
8
by: Lothar Scholz | last post by:
Because PHP5 does not include the mysql extension any more is there a chance that we will see more Providers offering webspace with Firebird or Postgres Databases ? What is your opinion ? I must...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.