Jump to content

Recommended Posts

i am trying to make a block element show/hide based upon one of two radio buttons in a group being checked.  think of the two radio buttons as a toggle switch.

 

here is my HTML radio group:

<tr>
<td class="form_label">Account Type:</td>            
<td>
                                                                                General Contractor: <input type="radio" name="acct_type" value="gc" id="input_gc" <?php if( $_SESSION['reg_acct_type'] == "gc" ){ echo"checked='checked'"; } ?> />
                                                                                Subcontractor: <input type="radio" name="acct_type" value="sc" id="input_sc" <?php if( $_SESSION['reg_acct_type'] == "sc" ){ echo"checked='checked'"; } ?> />
                                                                            </td>
                    </tr>

and here is the javascript:

function show_hide( curr_page )
{
    // REGISTER page
    if( curr_page == "register" )
    {
        alert("register");
        var input_gc     = document.getElementById("input_gc");
        var input_sc     = document.getElementById("input_sc");
        
        input_gc.onclick    = hide_select;
        input_sc.onclick    = show_select;       
        
        function hide_select()
        {
            alert("h");
            var sc_dropdown = document.getElementById("sc_dropdown");
            sc_dropdown.style.display     = 'none';
        }
        
        function show_select()
        {
            alert("s");
            var sc_dropdown = document.getElementById("sc_dropdown");
            sc_dropdown.style.display     = 'block';
        }
    } // close if( curr_page == "register" )
} // close function show_hide( curr_page )

i can get 'alert("register");' to trigger so i know the if statement is true

 

'alert("h");' and 'alert("s");' does not trigger when i click either radio button.

 

 

Link to comment
https://forums.phpfreaks.com/topic/281013-radio-button-event/
Share on other sites

Check, if the checked attribute is true.

 

So, the easiest way to achieve this (no tested)

 

js code:

<script>

function show_hide( curr_page )
{
    // REGISTER page
    if( curr_page == "register" )
    {
        
        var input_gc = document.getElementById("input_gc");
        var input_sc = document.getElementById("input_sc");
        
        if(input_gc.checked) hide_select();
        
        if(input_sc.checked) show_select();
        
    } // close if( curr_page == "register" )
    
    function hide_select()
        {
            alert("h");
            // procceed your logic
        }
        
        function show_select()
        {
            alert("s");
            // proceed your logic
            
        }
} // close function show_hide( curr_page )

</script>

Radio buttons:

<tr>
    <td class="form_label">Account Type:</td>            
    <td>
        General Contractor: <input type="radio" name="acct_type" value="gc" id="input_gc" />
        
        Subcontractor: <input type="radio" name="acct_type" value="sc" id="input_sc" />
    </td>
<tr><td><button onclick="show_hide('register')">Click me</button> </td></tr>
</tr>
Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/281013-radio-button-event/#findComment-1444302
Share on other sites

yes i tried using your suggestion as such;

function show_hide( curr_page )
{
    // REGISTER page
    if( curr_page == "register" )
    {
        var input_gc     = document.getElementById("input_gc");
        var input_sc     = document.getElementById("input_sc");
        var sc_dropdown = document.getElementById("sc_dropdown");
        
        // hide drop-down when page is refrshed or first loads
        sc_dropdown.style.display     = 'none';

        //input_gc.onclick    = hide_select;
        //input_sc.onclick    = show_select;
        if(input_gc.checked){ hide_select(); }
        if(input_sc.checked){ show_select(); }

        function hide_select( sc_dropdown )
        {
            var sc_dropdown = document.getElementById("sc_dropdown");
            sc_dropdown.style.display     = 'none';
        }
        
        function show_select( sc_dropdown )
        {
            var sc_dropdown = document.getElementById("sc_dropdown");
            sc_dropdown.style.display     = 'block';
        }
    } // close if( curr_page == "register" )
} // close function show_hide( curr_page )

as i said, my attempt is working in IE and Chrome but not FF.  when i tried the above that you suggested (which i had also tried yesterday at one point on my own) it stops working in all browsers. 

 

i have php writing errors to a log file and there are none in there.  well, there was one unrelated header error i fixed but now nothing.  logically, it seems to me like it should work.  i dont understand why FF is failing.  js drives me bonkers.

Link to comment
https://forums.phpfreaks.com/topic/281013-radio-button-event/#findComment-1444318
Share on other sites

ahhh, missed that.  i didnt think that would be an issue.  that why i suck with js.  so moving those two functions outside of my conditional makes it work with my version.  but i cannot get yours to work and validating a "checked" status seems like the better way to go.  so my questions are:

 

1. any idea why if i use your "if(input_sc.checked){ show_select(); }" conditional its still not working for me?

2. why do i need to move hide_select() and show_select() outside the conditional?

 

thanks for all of your help jazz.

 

this is working:

function show_hide( curr_page )
{
    // REGISTER page
    if( curr_page == "register" )
    {
        var input_gc     = document.getElementById("input_gc");
        var input_sc     = document.getElementById("input_sc");
        
        // hide drop-down when page is refrshed or first loads
        sc_dropdown.style.display     = 'none';

        input_gc.onclick    = hide_select;
        input_sc.onclick    = show_select;
        //if(input_gc.checked){ hide_select(); }
        //if(input_sc.checked){ show_select(); }

    } // close if( curr_page == "register" )
    
    function hide_select()
    {
        var sc_dropdown = document.getElementById("sc_dropdown");
        sc_dropdown.style.display     = 'none';
    }
    
    function show_select()
    {
        var sc_dropdown = document.getElementById("sc_dropdown");
        sc_dropdown.style.display     = 'block';
    }
} // close function show_hide( curr_page )
Link to comment
https://forums.phpfreaks.com/topic/281013-radio-button-event/#findComment-1444324
Share on other sites

Hey BuildMyWeb,

 


 

1. any idea why if i use your "if(input_sc.checked){ show_select(); }" conditional its still not working for me?

 

Are you sure that you're exactly running the same script? I don't think so ;)

 

 

2. why do i need to move hide_select() and show_select() outside the conditional?

 

 

So, what happens here?

 

First of all, you should be aware how the programming languages work.  

 

Well, when JavaScript encounters the function name that you already called in the script, it completes the instructions only if that function has been found somewhere inside the <script></script> tags. When the instructions have all been performed, JavaScript comes back and process the remainder of the script. But, actually you're creating that function inside the if statement after you're calling it. So, the parser of the language stops executing the script and says - " Hey, I cannot find the name of this function" and returns a false result. That's all. This was a logical error!

I highly recommend, you to wach this video created by Doug Crockford. Somewhere in the middle, he speaks about the proper way using of functions.

Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/281013-radio-button-event/#findComment-1444330
Share on other sites

i have the javascript in an external file that is referenced from the bottom of the head section.  then i was placing the following in the body section to call the function for this page:

<script type="text/javascript">
        show_hide("register");
    </script>

i removed both and simply pasted your javascript into the actual page, in the head section, and it still isnt working for me. 

Link to comment
https://forums.phpfreaks.com/topic/281013-radio-button-event/#findComment-1444344
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.