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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Javascript help

  • 26-11-2007 8: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,263 ✭✭✭✭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,263 ✭✭✭✭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