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 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation document.getElementByName?

    I'm trying to use this function in my javascript code, to get the value of a form element, by its name.
    but the javascript console tells me that this function does not exist.
    how does this work?
    please let me know.

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's [document.]getElementsByName (note the 's'). It returns an array of elements with that name, if any.

    You'd be better off giving your form elments id attributes and usung the getElementById call instead.

  • #3
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Could you post up your code please, in [code][/code] tags like this:
    [code]
    .... paste your code here
    [/code]

    many thanks

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #4
    New Coder
    Join Date
    Aug 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation here's my code

    I have a page, that contains a textarea and a bunch of <select> options.
    I also have javascript code. As I type keywords on my textarea, the <select> values that match it, get highlighted, which is what the javascript code does.

    Here's the javascript code and html code in one page.
    in my doSelection function, I had all the form elements hardcoded with their specific names. Right now, I want to upgrade my code so that I don't hardcode the names in the javascript code anymore. so I want to pass the names from the html code to the parameters in my javascript code, to make it usable for any code.
    I have the first two parameters figured out, but I'm not sure how to pass the last parameter, match_type.
    Please help me out.

    Code:
    <SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
    <!--
    
    
    function doSelection (typedText, selectControl, match_type)
    {
        // control is the textbox control
       //var typedText = control.value;
       //var selectControl = document.getElementById("metricsSelect");
       //when keyword starts with the typed text
       if(typedText=="")
       {
    	for (var i=0; i<selectControl.options.length; i++)
    	{
    		if(selectControl.options[i].selected == true)
    		{
    			selectControl.options[i].selected = false;
    		}
    	}	
       }
       else
       {
       	//if(document.getElementById("keywordSelect").checked)
    	if (match_type == "match_begin")
       	{
    		
    		for (var i=0; i<selectControl.options.length; i++)
    		{
    			if(selectControl.options[i].text.indexOf(typedText)==0)
    			{
    				selectControl.options[i].selected= true;
    	 		}
          			else
             		{
             				selectControl.options[i].selected = false;  // unselects non-matches
    	 		}
    		
    		}
       	}
       	//when keyword contains the typed text
       	//if(document.getElementById("keywordSelect2").checked)
    	if (match_type == "match_contain")
       	{
       	
    		for (var i=0; i<selectControl.options.length; i++)
       		{
    			//if (typedText!=null && typedText==selectControl.options[i].text.substring(0,typedText.length+1))
    	 		if(selectControl.options[i].text.indexOf(typedText)>=0)
             		{
    				selectControl.options[i].selected= true;
    	 		}
          			else
             		{
             			selectControl.options[i].selected = false;  // unselects non-matches
    	 		}
       		} 
       	}
       	//when keyword ends with the typed text
       	//if(document.getElementById("keywordSelect3").checked)
    	if (match_type == "match_end")
       	{
    	
    		for (var i=0; i<selectControl.options.length; i++)
    		{
    			var pos = selectControl.options[i].text.indexOf(typedText);
    			if(selectControl.options[i].text.substring(pos)==typedText)
    			{
    				selectControl.options[i].selected= true;
    	 		}
          			else
             		{
             			selectControl.options[i].selected = false;  // unselects non-matches
    	 		}
    		}
       	}
       }
       
    
    }
    
    //-->
    </SCRIPT>
    
    <html>
    <body>
    <title> keyword filtering </title>
    <form name="myform">
    <tr>
    
    <span>
    <input type="checkbox" name="metricsSelect" value="match_begin" class="checkbox" id="keywordSelect"> starts with
    &nbsp&nbsp<input type="checkbox" name="metricsSelect" value="match_contain" class="checkbox" id="keywordSelect2"> contains
    &nbsp&nbsp<input type="checkbox" name="metricsSelect" value="match_end" class="checkbox" id="keywordSelect3"> ends with
    &nbsp
    <input type="text" name="metrics" size="40" value=""
    onkeyup="doSelection(this.value, document.getElementById('metricsSelect'), this.form.metricsSelect.value);" style="query-table-item" id="metricsTxt">
    </span>
    <br><br>
    <td>
    <select name="metrics" multiple="multiple" size="5" style="width:450" id="metricsSelect">
    <option value="100000018">approved</option>
    <option value="100000013">auto_purchase</option>
    <option value="100000035">auto_purchase_external</option>
    <option value="100000033">auto_purchase_internal</option>
    <option value="100000070">auto_purchase_internal_approved</option>
    <option value="100000245">auto_purchase_lease</option>
    <option value="100000246">auto_purchase_lease_external</option>
    <option value="100000248">auto_purchase_lease_internal</option>
    <option value="100000247">auto_purchase_lease_internal_approved</option>
    <option value="100000239">auto_purchase_private</option>
    <option value="100000240">auto_purchase_private_external</option>
    <option value="100000242">auto_purchase_private_internal</option>
    <option value="100000241">auto_purchase_private_internal_approved</option>
    </br></br>
    </form>
    </body>
    </html>

  • #5
    New Coder
    Join Date
    Aug 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, I've figured it out.


  •  

    Posting Permissions

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