[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

How to get element value from html when using an external JS file

bobspero

9/7/2014 1:00:00 AM

I have an external js file in my php project, RCC is a dropdown. I want to get the value of RCC from an external java script file, can someone assist with what I am missing?

var x = document.forms["index"]["RCC"].value;
alert(x);
1 Answer

Evertjan.

9/7/2014 7:57:00 AM

0

bobspero@gmail.com wrote on 07 sep 2014 in comp.lang.javascript:

> I have an external js file in my php project, RCC is a dropdown. I want
> to get the value of RCC from an external java script file, can someone
> assist with what I am missing?

It is a Javascript script, I hope,
not a [non-existing] Java script?

Your Q has nothing to do with php,
as it is about clientside js and the DOM.

Dropdown? I stipulate you mean an html <select>?

What browser do you use, Netscape?

> var x = document.forms["index"]["RCC"].value;

Stipulating that:

"index" is the name of the <form>, not the id
"RCC" is the name of rhe <select>, not the id

This works fine:

<script type='text/javascript'>
function a() {
var mySelect = document.forms.index.RCC;
var selVal = mySelect.value;
alert(selVal);
};
</script>
<button onclick='a();'>x</button>

[
Antique browsers need additional scripting:
var selVal = mySelect.options[mySelect.selectedIndex].value;
]

==========================

You could also have the value prepared onchange:

<script type='text/javascript'>
var selVal = 'notyet selected, suspect "0"'; // global
</script>

<select onchange='selVal = this.value;'>
<option value='0'>-- Select --</option>
<option value='x1'>text 1</option>
<option value='x2'>text 2</option>
<option value='x3'>text 3</option>
</select>
<button onclick='alert(selVal);'>What value?</button>

============================

Try as a joke:
<button onclick='this.innerHTML=selVal;'>What value?</button>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)