...

View Full Version : simple code for selecting matching text in a listbox



asidana
04-28-2008, 07:05 PM
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[i].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. eh 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

mjlorbet
04-28-2008, 07:30 PM
Get rid of String(...) and just leave it as the element within (ie. String(S.options[i].value).substring(0,6)) becomes S.options[i].value.substring(0,6)) ) , that's one of the cooler (and worst) features of javascript is the loose typing, it can be both your best friend and worst enemy at the same time.

another thing in the logic for your for loop
i <= L-1 is equivalent to i < L being that you're operating on the integers, me, i'd rather leave out the -1 and equals, 2 reasons 1) less code to send 2) i never implicitly trust (for no good reason, mind you) operator precedence unless i wrap it in parenthesis.

asidana
04-28-2008, 08:03 PM
sorted 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[i].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:";
}
}

mjlorbet
04-28-2008, 08:08 PM
function selectMatching(x)
{
var S = document.myform.elements['mymonth'];
var L = S.options.length;
for (var i=0; i <= L-1; i++)
{
if (x.value.substring(0,6) == S.options[i].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:";
}
}

please wrap your code inside code tags, it's the # in the editor. also, i'm not sure what you mean by sdorted, so... if you fixed it then good, otherwise you should probably have a look at the error console in opera and post the error message.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum