Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript help

  • 26-11-2007 09:19AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 21,278 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 21,278 ✭✭✭✭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