SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    attempting to see which input(s) has been clicked

    I am using the Prototype.js library (1.6), and I am attempting to create a javascript function to see which of my input boxes are checked.

    Here is my function:
    Code JavaScript:
    window.onload = function() {
     
        var inputs;
     
    	inputs = $('filters').getElementsByTagName("input");
     
    	for (var i=0;i<inputs.length;i++) {
    		if (inputs[i].checked) {
     
    			 alert(inputs[i].checked);
     
    		}
    	}
     
    } //End window.onload()

    However, no matter what input I select, it never alerts me.

    Any ideas?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know about the library, but it looks like a simple problem-

    Your code is written to alert you if any inputs are checked when the page loads,
    there is no event handling for later checking.

    Your return value, by the way, will be a series of alerts that say 'true', not references to the checked elements.

  3. #3
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this too:

    Code:
    var inputs = document.getElementsByTagName("input");
    
    for (var i=0; i<inputs.length; i++)
    {
     	if (inputs[i].onclick) {
    		
    		alert(inputs[i].checked);
    		
    	}
    }
    Still, nothing.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    onload=function(){
    var inputs= document.getElementsByTagName("input");
    var L=inputs.length;
    for (var i= 0; i< L; i++){
    	var inp= inputs[i];
    	inp.onclick= function(e){
    		e= window.event || e;
    		var who= e.target || e.srcElement;
    		var s= who.name || who.id || 'input_'+i;
    		if(who.checked)alert(s +' checked');
    	}
    }
    }

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where have you placed the code in the page? At the beginning of the (before body tag) or at the end of document? I think before loading all the HTML you cannot check them. So try to put the JS code just before the closing body tag and see.
    Code javascript:
    var inputs = document.getElementsByTagName("input");
    for (var i=0; i<inputs.length; i++){
     	if (inputs[i].type == "checkbox" && inputs[i].checked == true) {
    		alert('checkbox ' + (i+1) + ' is checked.');
    	}
    }
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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