Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript help

Options
  • 26-11-2007 9:19am
    #1
    Registered Users Posts: 500 ✭✭✭


    Hey folks,

    I have a html page which contains a list of bulleted locations
    • Dublin
    • Cork
    • Galway
    • Laois
    • etc

    On top of this page is a textbox and button. When the user enters a county intothe textbox i want to use javascrpt to check if the value exists in the list. If it does throw us a msgbox.
    Can anyone help.


Comments

  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Here you go - this function gets all the <li> tags contained in a specified <ul> tag and searches the innerHTML of each one for the value in the textbox.

    [html]
    <html>
    <head>
    <title>Search page</title>
    <script type="text/JavaScript">
    function rightTrim(sString)
    {
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
    }

    function searchPage()
    {
    var sSearch = document.getElementById("txtSearch").value;
    if (sSearch != "")
    {
    var theList = document.getElementById("locations");
    var theLocations = theList.getElementsByTagName("li");

    var i;

    var foundFlag = false;

    for (i = 0; i < theLocations.length; i++)
    {
    if (rightTrim(sSearch) == rightTrim(theLocations.innerHTML))
    {
    foundFlag = true;
    break;
    }
    }

    if (foundFlag)
    {
    alert(sSearch + " was found");
    }
    else
    {
    alert(sSearch + " was not found");
    }
    }
    }
    </script>
    </head>
    <body>
    <input type="text" id="txtSearch" value=""/><input type="button" value="go" onclick="searchPage()"/>
    <ul id="locations">
    <li>Dublin</li>
    <li>Cork</li>
    <li>Galway</li>
    <li>Laois</li>
    <li>Meath</li>
    </ul>
    </body>
    </html>
    [/html]


  • Registered Users Posts: 500 ✭✭✭warrenaldo


    thanks for this - just what i was looking for - have been searching the web for this:
    <b>
    var theList = document.getElementById("locations");
    var theLocations = theList.getElementsByTagName("li");
    </b>
    does the trick. thanks again.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    warrenaldo wrote: »
    thanks for this - just what i was looking for - have been searching the web for this:
    <b>
    var theList = document.getElementById("locations");
    var theLocations = theList.getElementsByTagName("li");
    </b>
    does the trick. thanks again.

    No bothers :)


Advertisement