simple code for selecting matching text in a listbox

N

.nLL

got below script, works fine with IE but fails on opera. my js knowledge is
very limited and can't find whats wrong with it.

----------------------------------------------
function selectMatching(x)
{
var S = document.getElementById("mymonth");
var L = S.options.length;
for (var i=0; i <= L-1; i++)
{
if ((String(x.value).substring(0,6)) ==
String(S.options.value).substring(0,6)) {S.selectedIndex=i};
}
}
-------------------------------------

and i use it in below form

-------------------------------------------------
<form method="post" name="myform" action="default.asp">
<p>
Select Month: <select name="mymonth"
onChange="document.getElementById('myday').selectedIndex=0">
<option value="20080401" selected="selected" >April-2008 (25299)</option>
<option value="20080301" >March-2008 (2)</option>
<option value="20080201" >February-2008 (1)</option>
</select>
Day: <select name="myday" onChange="selectMatching(this);">
<option value="0" selected="selected">All days</option>
<option value="20080401" >1-Tuesday (1)</option>
<option value="20080420" >20-Sunday (2)</option>
<option value="20080421" >21-Monday (1)</option>
<option value="20080425" >25-Friday (25293)</option>
<option value="20080427" >27-Sunday (2)</option>
</select>
<input type="submit" value="View"/>
</p>
</form>
---------------------------------------------------



what i do is. if user selects from months menu script resets days menu to
all days (doesn't work on opera)

if user changes days menu function finds matching month from the first list
and selects it. Although days has 2 more numbers ad the end by substring i
cut that part.

can anyone offer another solution that would work on opera too without
changing current form and its values?

thanks
 
N

.nLL

sdorted with
-----------------------------------
function selectMatching(x)
{
var S = document.myform.elements['mymonth'];
var L = S.options.length;
for (var i=0; i <= L-1; i++)
{
if ((String(x.value).substring(0,6)) ==
String(S.options.value).substring(0,6)) {S.selectedIndex=i};
}
}

and

function setContent() {
if(document.detailsform.paymentMethod.value=="1")
{
document.getElementById('paymentMethodExplain').innerHTML= "Registered
Paypal e-mail:";
}
if(document.detailsform.paymentMethod.value=="2")
{
document.getElementById('paymentMethodExplain').innerHTML= "Bank account
details:";
}
if(document.detailsform.paymentMethod.value=="3")
{
document.getElementById('paymentMethodExplain').innerHTML= "Full name and
address:";
}
}
 
Á

Álvaro G. Vicario

..nLL escribió:
got below script, works fine with IE but fails on opera. my js knowledge
is very limited and can't find whats wrong with it.

It doesn't work in Firefox either. So I'd say it only works in IE.

var S = document.getElementById("mymonth");

This line finds an HTML tag with id="mymonth". There isn't any on the
page. Replace it with:

var S = document.forms["myform"].mymonth;

This way you call a form element with name "mymonth" in a form with name
"myform":
<form method="post" name="myform" action="default.asp">
<p>
Select Month: <select name="mymonth"

Same here:
onChange="document.getElementById('myday').selectedIndex=0">

onChange="document.forms['myform'].myday.selectedIndex=0"
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top