469,643 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

selecting an index

i use this but select the index by name??

document.form1.table_name.selectedIndex= 2;
what i want

document.form1.table_name.selectedIndex= "news";


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Oct 25 '05 #1
4 1056
Ross wrote:
i use this but select the index by name??

document.form1.table_name.selectedIndex= 2;

If this works, then form1 is the name or id of a form, table_name is the
name or id of a select element that is a child of form1 and it has at
least 3 option elements.

The above will select the 3rd option.

what i want

document.form1.table_name.selectedIndex= "news";


If the above is correct, you can't do it. An option element can't have
a name attribute, though it can have an ID.

If what you want to do is select the option with either text or value of
'news', then loop through the elements looking for the right one. Once
you find it, set it to selected. Something like:
var opts = document.form1.table_name.options;
for (var i=0, len=opts.length; i<len; ++i){
if ('news' == opts[i].value){
opts[i].selected = true;
return;
}
}
// Just in case none match?
opts[0].selected = true;
If you want to match the text content, then use:

...
if ('news' == opt[i].text){
...

[...]

--
Rob
Oct 25 '05 #2
VK

Ross wrote:
i use this but select the index by name??

document.form1.table_name.selectedIndex= 2;
what i want

document.form1.table_name.selectedIndex= "news";


....
<option selected value="news">Recent News</option>
....

1. document.form1.table_name[form1.table_name.selectedIndex].text='Hot
News';

(Changes "Recent News" to "Hot News")

2.
document.form1.table_name[form1.table_name.selectedIndex].value='hot_news';

(Changes "news" to "hot_news")

Oct 25 '05 #3
VK
Seems I did not get your problem right.

Oct 25 '05 #4
VK wrote on 25 okt 2005 in comp.lang.javascript:
Seems I did not get your problem right.


But I have no problem.

[please always quote on usenet, this is not email]

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Axial | last post: by
18 posts views Thread by booner | last post: by
1 post views Thread by don | last post: by
8 posts views Thread by Kari Lavikka | last post: by
5 posts views Thread by Miquel van Smoorenburg | last post: by
13 posts views Thread by lane straatman | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.