Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple code for selecting matching text in a listbox

    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

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    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.
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorted with
    -----------------------------------
    Code:
    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:";
    	}
    }
    Last edited by asidana; 04-28-2008 at 08:15 PM. Reason: typo

  • #4
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
    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.
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •