We help IT Professionals succeed at work.

Javascript Automatically click radio button on load if hidden field is Not empty

swedesamurai
swedesamurai asked
on
Medium Priority
982 Views
Last Modified: 2012-05-06
Hello Experts,
I have a Javascript that evaulates whether a field i full or empty and shows a show hide div by name.  I have been trying to modify it so that a radio button would also be checked at the same time. Does anyone have an ideas of how I could do this  The code snippet below is how the script evaluates field KDKK1 for presence and the shows a division id named pay2.  I would also like it to check a radio button named CC at the same time.  Is this possible.  I start this button in body elemt of the form so that it automatically evaluates the field on load (KDNKK1) is an imported value from PHP.  Any help would greatly be appreciated.  Thanks.
function changeDisplay1()
    {
        var elm = document.getElementById('KDNKK1');
		var vis = ( (elm.value == '') ? 'none' : '');
		
        elm = document.getElementById('pay2');
        if (elm != null)
		    elm.style.display = vis;
    }

Open in new window

Comment
Watch Question

function changeDisplay1()
    {
        var elm = document.getElementById('KDNKK1');
            var vis = ( (elm.value == '') ? 'none' : '');
            
        elm = document.getElementById('pay2');
        if (elm != null)
                elm.style.display = vis;
        elm = document.getElementById('CC');
        if (elm != null)
                elm.checked = vis=='';
         
  }

this checks radiobutton if vis equals '', and do not checks it, if vis not equals '' (e.g. it is 'none')

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
Hello Swede,

Please Have a look at the following articles.

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20696473.html

http://rarescript.com/javascript-change-radio-button-properties/

Best Regards,

Mohamed Allam
Senior Software Engineer
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
If there is an onClick handler on the radio, change to
if (vis=="") elm.click();

Author

Commented:
Because I have a rock for a brain it takes me a while to undrstand things.  Your help on this was really great.  The basic code from ludofulop was helpful.  The info by Allamz helped me learn, and the final croup de grace was the line of code from mplungian that solved my exact problem.  Thanks Experts you saved me yet again
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.