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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I get a form element to notify me of its element number within a form???

    I can find an element using a loop to check through all the elements within a form but I need a way that an element (eg a checkbox which is one of many of the same name) can notify me of its array position when i click it.

    The checkbox is defined within 'myform' as follows in a php loop which builds up the form:
    <input type='checkbox' name='addme' id='addme' onClick='addthis()'>

    in this way I create a form of club members with a checkbox whcih I intend to use to add/delete members to a mail list.

    I can get addthis() to activate with general debug messages but it will not report document.myform.addme.length. Just one of my issues as I dont know how many elements (ie club members in the form) to loop through.

    I want to click on any checkbox and use its array position to set another array element with a corresponding value.
    Any suggestion gratefully accepted - i am a newbie after all...

  • #2
    Regular Coder
    Join Date
    Nov 2004
    Location
    The land of chocolate
    Posts
    226
    Thanks
    1
    Thanked 16 Times in 16 Posts
    Code:
    <script language="JavaScript" type="text/JavaScript">
    function showIndex(field) {
    	var form = document.blah;
    	for(i=0;i<form.elements.length;i++) {
    		if(form.elements[i].id == field) {
    			alert("The index of this field is " + i);
    			break;
    		}
    	}
    }
    </script>
    
    ...
    
    <form name="blah">
    <input type="text" name="fieldname" id="field1" onclick="showIndex(this.id);"><br>
    <input type="text" name="fieldname" id="field2" onclick="showIndex(this.id);"><br>
    <input type="text" name="fieldname" id="field3" onclick="showIndex(this.id);"><br>
    </form>
    Names can be the same (and should be in case of checkboxes and radio buttons) but id's should be unique. I hope this gets you any further!
    Last edited by snowieken; 02-16-2009 at 01:08 PM.
    -Snow

    Susie, if you want to see your doll again, leave $100 in this envelope by the tree out front. Do NOT call the police. You CANNOT trace us. You CANNOT find us.

    Sincerely,
    - Calvin.


  •  

    Tags for this Thread

    Posting Permissions

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