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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post

    Check if a select value exists?

    Hey guys..... I found out how you can check if an array value exists but I can't seem to get it to work with the options array for some reason.

    Any help on how I can find out if a certain select value exists?

    Thanks!

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    this is the method to check for existance:
    Code:
    Array.prototype.contains = function (zValue)
    			{
    				for(var i=0; i<this.length; i++)
    				{
    					if(this[i] == zValue)
    					{
    						return true;
    					}
    				}
    				return false;
    			}
    This is the usage:
    Code:
    var arr = ["1","2","3","4","5"];
    window.onload = function()
    			{
    				alert(arr.contains("1"))
    				alert(arr.contains("2"))
    				alert(arr.contains("9"))
    			}

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yeah.. I tried that code using something like this..

    PHP Code:

    <select id="list">
    // Options
    </select>

    var 
    cat_id 12345;
    var list = 
    document.getElementById('list');
    var 
    options = list.options;

    if (list.
    contains(cat_id) === false) {


    ..but it gives the error that "list.contains" isn't a function.

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Because list is not an array and the method I have given you adds a method to the javascript Array object.

    So I think that your initial post was misleading.

    try this:
    Code:
    var cat_id = 12345;
    var list = document.getElementById('list');
    for(var i=0; i<list.options.length; i++)
    {
       if(list.options[i].value =cat_id)
      {
    //found it
      }
    }
    but if you need to add a method like in my previous example you should use the Prototype.js and then use the following code to add the method contains:

    Code:
    var contains= function (zValue)
    			{
    				for(var i=0; i<this.options.length; i++)
    				{
    					if(this.options[i].value == zValue)
    					{
    						return true;
    					}
    				}
    				return false;
    			}
    Element.addMethods("SELECT",contains);

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok..... so where do I add that new code you supplied for use with prototype.js?

    Also.... can you please provide a usage example for my needs?

    Thanks!

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    First you should include the prototype.js in your code:
    Code:
    <script type="text/javascript" src="Protoype.js"></script>
    and then in another script tag you do the following:
    Code:
    <script type="text/javascript">
    var contains= function (zValue)
    			{
    				for(var i=0; i<this.options.length; i++)
    				{
    					if(this.options[i].value == zValue)
    					{
    						return true;
    					}
    				}
    				return false;
    			}
    
    window.onload = function()
    {
    Element.addMethods("SELECT",contains);
    var cat_id = 12345;
    var list = document.getElementById('list'); 
    alert(list.contains(cat_id))
    }
    
    </script>

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yep... that's similar to what I tried... yet I keep getting the same error message that it isn't a function.

    Here is some of the code..

    I have this in a seperate js file that I include..

    PHP Code:
    var contains= function (zValue)
                {
                    for(var 
    i=0i<this.options.lengthi++)
                    {
                        if(
    this.options[i].value == zValue)
                        {
                            return 
    true;
                        }
                    }
                    return 
    false;
                } 
    Then this is part of the code...

    PHP Code:
    function addCats() {
        
        
    Element.addMethods("SELECT",contains);
        
        
    // Set the search results box
        
    var cats document.getElementById('cat_list');

        
    // Set the selected categories list that we are adding to..
        
    var sel_cats document.getElementById('cat_sel');
        
        
    // Loop through categories from the search results select box
        
        
    for (0cats.lengthi++) {
            
            
    // Check if the cat is selected
            
            
    if (cats.childNodes[i].selected == true) {
                
                
    // Set this category's values to some variables
                
    var cat_id cats.childNodes[i].getAttribute('value');
                var 
    cat_name cats.options[i].text;
                
                
    alert(sel_cats.contains(cat_id));
                return;

    // More code 

  • #8
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    did you include the file prototype.js before any other js file in you html

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yes I did..


  •  

    Posting Permissions

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