473,388 Members | 1,426 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,388 software developers and data experts.

Accessing a HTML element using javacript variable

Hi All,

I am passing ID of the HTML SELECT element to a javascript function.
In that function I need to access the value selected in that select box.
I tried the following code. but it didn't work.

Expand|Select|Wrap|Line Numbers
  1. function save()
  2.    var x='bid';
  3.    res=document.bform.x.options[document.x.selectedIndex].value;
  4.    alert(res);
  5. }
  6.  
[HTML]
<form name="bform" >
<select id="bid" name="bid">
<option value="1">1
<option value="2">2
</select>
<input type="button" onClick="save();">
</form>
[/HTML]

My problem is , I am having id value of the select box in a variable.
How can I use that value while calling "document.frm_name.select_element_name.selectedInd ex;"

Pl. do reply me.
Aug 22 '07 #1
2 1889
acoder
16,027 Expert Mod 8TB
Replace
Expand|Select|Wrap|Line Numbers
  1. .x.
with
Expand|Select|Wrap|Line Numbers
  1. [x]
in your code or try:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(x).value;
Aug 22 '07 #2
gaya3
184 100+
Hi All,

I am passing ID of the HTML SELECT element to a javascript function.
In that function I need to access the value selected in that select box.
I tried the following code. but it didn't work.

Expand|Select|Wrap|Line Numbers
  1. function save()
  2.    var x='bid';
  3.    res=document.bform.x.options[document.x.selectedIndex].value;
  4.    alert(res);
  5. }
  6.  
[HTML]
<form name="bform" >
<select id="bid" name="bid">
<option value="1">1
<option value="2">2
</select>
<input type="button" onClick="save();">
</form>
[/HTML]

My problem is , I am having id value of the select box in a variable.
How can I use that value while calling "document.frm_name.select_element_name.selectedInd ex;"

Pl. do reply me.

Hi,
Instead of giving like
res=document.bform.x.options[document.x.selectedIndex].value;

u may give like....
res=document.bform.x.options[document.bform.x.selectedIndex].value;

i have tried out.. its working fine..

-Thanks,
Hamsa
Aug 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Julius Mong | last post by:
Dear all, I have something like this: <html... > <embed ...> </html> Am I out of luck if I wanted to access the embedded DOM and manipulate its content? Or if I have:
3
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to...
3
by: Sandros | last post by:
Background: I'm collecting usability statistics for a group of applications. Each count has the following attributes: date, application, major heading, minor heading, count. My intent is to pull...
5
by: Craig Anderson | last post by:
Can anyone tell me the best way to access a hidden object in a form? I could use a hard-coded index to the elements of the form, but it's too easy to add something before the hidden object and mess...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
19
by: k.karthikit | last post by:
Hello all, In some hidden variable (<input type="hidden" name="hiddenId" value="test" /> ,i stored some value.I accessed the value "test" using var id = document.getElementById( 'hiddenId' );...
7
by: rein.petersen | last post by:
Hey All, I was wondering if there were a way for a script to access it's parent tag without having to use the document.all.tags method which doesn't necessarily identify it if there are more...
6
by: libsfan01 | last post by:
hi all! is it possible to change an element type with javacript for example onclick to change a text box to a hidden element or to a textarea? kind regards marc
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...
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.