Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: radiobutton value with id storing in a variable

  1. #1
    Join Date
    Jan 2012
    Location
    Netherlands, Hoogezand
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default radiobutton value with id storing in a variable

    Hi all,

    This probably has a simple solution, but i am not seeing it anymore.
    I want the value from a radio button inserted in a variable, but all i get is "undefinied" as an answer.

    My radiobuttons are in a form called 'personalia':
    Code:
    <input type="radio" name="geslacht" value="m" id="geslacht_m" onchange="personalia_showform();"/><label for="geslacht_m">Man</label>
    <input type="radio" name="geslacht" value="v" id="geslacht_v" onchange="personalia_showform();"/><label for="geslacht_v">Vrouw</label>

    Then when i hit the submit button, i am calling a javascript which should be entering the value into a variable.
    My variable declaration:
    Code:
    var pers_gesl = document.personalia.geslacht.value;
    Then when i show the pers_gesl on my screen it only says: undefinied.
    What am i doing wrong?

    Thanks in advance.

  2. #2
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    html

    Code:
    <input type="radio" name="geslacht" value="m" id="geslacht_m" onchange="personalia_showform(this);"/><label for="geslacht_m">Man</label>
    <input type="radio" name="geslacht" value="v" id="geslacht_m" onchange="personalia_showform(this);"/><label for="geslacht_v">Vrouw</label>


    javascript

    Code:
    <script type="text/javascript">
    function personalia_showform(obj){
    	var pers_gesl = obj.value;
    }
    </script>


    is this is not what you are looking for,. can you please post your whole code here?
    Last edited by regicidedelferoz; 01-04-2012 at 03:11 AM. Reason: edited

  3. #3
    Join Date
    Jan 2012
    Location
    Netherlands, Hoogezand
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    No that's not it, but thanks anyway.
    I'll post all the necessary code down below. I am not going to post all code, cause that's more than 700 lines of HTML-code alone.

    HTML
    Code:
    <form name="personalia" action="personalia.htm" method="post" enctype="text/plain">
    <div class="antw"><input type="radio" name="geslacht" value="m" id="geslacht_m" onchange="personalia_showform();"/><label for="geslacht_m">Man</label></div>
    <div class="antw"><input type="radio" name="geslacht" value="v" id="geslacht_v" onchange="personalia_showform();"/><label for="geslacht_v">Vrouw</label></div>
    <div class="antw"><input type="radio" name="geslacht" value="st_mv" id="geslacht_mv" onchange="personalia_showform();"/><label for="geslacht_mv">Man/Vrouw-stel</label></div>
    <div class="antw"><input type="radio" name="geslacht" value="st_vv" id="geslacht_vv" onchange="personalia_showform();"/>	<label for="geslacht_vv">Vrouw/Vrouw-stel</label></div>
    <div class="antw"><input type="radio" name="geslacht" value="st_mm" id="geslacht_mm" onchange="personalia_showform();"/><label for="geslacht_mm">Man/Man-stel</label></div>
    
    <input type="submit" value="Bewaren" onclick="formulier_controle_personalia();" onkeypress="formulier_controle_personalia();">
    </form>
    The function personalia_showform() let's me show another set of questions that's related to the gender of the person, so that's not the code that needs to be altered.
    The function formulier_controle_personalia() is the function that has the declaration of the variables. And the id from the radio button is necessary for the <label> part.

    Javascript:
    Code:
    function formulier_controle_personalia()
    {
    	var pers_gesl = document.personalia.geslacht.value;
    
           alert("Personalia wordt gecontroleerd!\npers_gesl: " +pers_gesl)
    }
    Last edited by lampie1978; 01-04-2012 at 10:55 AM.

  4. #4
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    try this one sir,.

    Code:
    function formulier_controle_personalia(){
    	var pers_gesl = document.personalia.geslacht.value;
    	i = document.getElementsByName('geslacht').length;
    	for(ctr=0; ctr<=i; ctr++){
    		if(document.personalia.geslacht[ctr].checked){
    			pers_gesl = document.personalia.geslacht[ctr].value;
    			alert("Personalia wordt gecontroleerd!\npers_gesl: " +pers_gesl)
    		}
    	}
    }

  5. #5
    Join Date
    Jan 2012
    Location
    Netherlands, Hoogezand
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    ok that works.. thanks...

    but how do i implement this for more then 20 different variables that all come from radio or check-buttons on the same page??
    and still throw in a other checks on those variables after they have been declared...
    and i really don't like to nest 20 or more functions like these within each other?? do i need an array or something??

    i know, i'm making it difficult on you..
    but this way i am learning from it also..

  6. #6
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    is your radio buttons and check buttons have the same names?,.

    if so, you have nothing to worry,. the code above can do it,.

  7. #7
    Join Date
    Jan 2012
    Location
    Netherlands, Hoogezand
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    nope all groups off radio or checkbuttons have different names for example:

    Code:
    var pers_rel = document.personalia.relatie.value;
    var pers_p1_post = document.personalia.p1_postuur.value;
    var pers_p1_ogen = document.personalia.p1_ogen.value;
    var pers_p2_post = document.personalia.p2_postuur.value;
    var pers_p2_ogen = document.personalia.p2_ogen.value;
    and many more where these came from..

  8. #8
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    try this one

    Code:
    function formulier_controle_personalia(){
    	radios = document.getElementsByTagName('input');
    	for (x=0; x<radios.length; x++) {
            	if(radios[x].checked){
    			rdoName = radios[x].name;
    			rdoValue = radios[x].value;
    			
    			/* declaring variables */
    			if(rdoName==name_of_a_radio_group_name)
    				var variable_name = rdoValue
    			else if(rdoName==another name of radio group name) then
    				var another_variable = rdoValue
    		}
    	}
    }
    feel free to ask question.. hope this one is the code you are looking for,.
    Last edited by regicidedelferoz; 01-07-2012 at 02:29 AM. Reason: indents

  9. #9
    Join Date
    Jan 2012
    Location
    Netherlands, Hoogezand
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi again,

    Well it isn't working for so far. I really thought u had it this time, the code seems plausible.

    I tried every variation on the code i possibly know, but nothing has worked so far..

    Maybe it's better for me to post both files for u to try it yourselfs.
    So u can find the .htm and .js in the ZIP-file in this post also.

    Thanks again.

  10. #10
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    try this one,.

    Code:
    /************************************************************************/
    /*                                              			*/
    /*        formulier controles						*/
    /*                                              			*/
    /************************************************************************/
    
    function formulier_controle_personalia()
    {
    	radios = document.getElementsByTagName('input');
    	for (x=0; x<radios.length; x++)
    	{
    		if(radios[x].checked)
    		{
    			rdoName = radios[x].name;
    			rdoValue = radios[x].value;
    							
    			/* declaring variables */
    			if (rdoName=='geslacht') var pers_gesl = rdoValue
    			else if(rdoName=='relatie') var pers_rel = rdoValue
    			else if(rdoName=='p1_postuur') var pers_p1_post = rdoValue
    			else if(rdoName=='p1_ogen') var pers_p1_ogen = rdoValue
    			else if(rdoName=='p1_haar') var pers_p1_haar = rdoValue
    			else if(rdoName=='p1_huidskleur') var pers_p1_huid = rdoValue
    			else if(rdoName=='p1_roken') var pers_p1_roken = rdoValue
    			else if(rdoName=='p1_tatoeage') var pers_p1_tatoe = rdoValue
    			else if(rdoName=='p1_piercing') var pers_p1_piercing = rdoValue
    			else if(rdoName=='p1_penis') var pers_p1_penis = rdoValue
    			else if(rdoName=='p1_geaardheid') var pers_p1_geaardheid = rdoValue
    			else if(rdoName=='p1_borsten') var pers_p1_borsten = rdoValue
    			else if(rdoName=='p1_geschoren') var pers_p1_scheren = rdoValue			
    			else if(rdoName=='p2_postuur') var pers_p2_post = rdoValue
    			else if(rdoName=='p2_ogen') var pers_p2_ogen = rdoValue
    			else if(rdoName=='p2_haar') var pers_p2_haar = rdoValue
    			else if(rdoName=='p2_huidskleur') var pers_p2_huid = rdoValue
    			else if(rdoName=='p2_roken') var pers_p2_roken = rdoValue
    			else if(rdoName=='p2_tatoeage') var pers_p2_tatoe = rdoValue
    			else if(rdoName=='p2_piercing') var pers_p2_piercing = rdoValue
    			else if(rdoName=='p2_penis') var pers_p2_penis = rdoValue
    			else if(rdoName=='p2_borsten') var pers_p2_borsten = rdoValue
    			else if(rdoName=='p2_geaardheid') var pers_p2_geaardheid = rdoValue
    			else if(rdoName=='p2_geschoren') var pers_p2_scheren = rdoValue
    			
    			var pers_nick = document.personalia.nickname.value;
    			var pers_email = document.personalia.email.value;
    			var pers_ww = document.personalia.wachtwoord.value;
    			var pers_wwchk = document.personalia.wachtwoord_check.value;
    			var pers_prov = document.personalia.provincie.value;
    			var pers_p1_gebdat = document.personalia.p1_dag.value +"/"+ document.personalia.p1_maand.value +"/"+ document.personalia.p1_jaar.value;
    			var pers_p1_lengte = document.personalia.p1_lengte.value;
    			var pers_p2_gebdat = document.personalia.p2_dag.value +"/"+ document.personalia.p2_maand.value +"/"+ document.personalia.p2_jaar.value;
    			var pers_p2_lengte = document.personalia.p2_lengte.value;
    		}
    	}
    		
    	alert("Personalia wordt gecontroleerd!\npers_nick: " +pers_nick+ "\npers_email: " +pers_email+ "\npers_ww: " +pers_ww+ "\npers_wwchk: " +pers_ww
    	      + "\npers_prov: " +pers_prov+ "\npers_gesl: " +pers_gesl+ "\npers_rel: " +pers_rel+ "\npers_p1_gebdat: " +pers_p1_gebdat
    	      + "\npers_p1_lengte: " +pers_p1_lengte+ "\npers_p1_post: " +pers_p1_post+ "\npers_p1_ogen: " +pers_p1_ogen+ "\npers_p1_haar: " +pers_p1_haar
    	      + "\npers_p1_huid: " +pers_p1_huid+ "\npers_p1_roken: " +pers_p1_roken+ "\npers_p1_tatoe: " +pers_p1_tatoe+ "\npers_p1_piercing: " +pers_p1_piercing
    	      + "\npers_p1_penis: " +pers_p1_penis+ "\npers_p1_borsten: " +pers_p1_borsten+ "\npers_p1_geaardheid: " +pers_p1_geaardheid
    	      + "\npers_p1_scheren: " +pers_p1_scheren+ "\npers_p2_gebdat: " +pers_p2_gebdat+ "\npers_p2_lengte: " +pers_p2_lengte+ "\npers_p2_post: " +pers_p2_post
    	      + "\npers_p2_ogen: " +pers_p2_ogen+ "\npers_p2_haar: " +pers_p2_haar+ "\npers_p2_huid: " +pers_p2_huid+ "\npers_p2_roken: " +pers_p2_roken
    	      + "\npers_p2_tatoe: " +pers_p2_tatoe+ "\npers_p2_piercing: " +pers_p2_piercing+ "\npers_p2_penis: " +pers_p2_penis
    	      + "\npers_p2_borsten: " +pers_p2_borsten+ "\npers_p2_geaardheid: " +pers_p2_geaardheid+ "\npers_p2_scheren: " +pers_p2_scheren);
    }

  11. The Following User Says Thank You to regicidedelferoz For This Useful Post:

    lampie1978 (01-10-2012)

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
  •