Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JS question

  • 01-06-2004 9:26am
    #1
    Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭


    Ok I have a very simple question

    In javascript I have a very simple form

    consisting of two radio buttons and a button

    when you click the button it should call a JS function which will tell you what radio button you clicked

    see
    <script language="JavaScript">
    function doit() {
    	if (sex.m.checked) 
    		alert('male');
    	else if (sex.f.checked) 
    		alert('female');
    	else
    		alert('something wrong');
    }
    </script>
    <form>
    <input type="radio" name="sex" id="m" value="male" checked> Male
    <br>
    <input type="radio" name="sex" id="f" value="female"> Female
    <br>
    <input type="button" value="Hello world!" onclick=doit()>
    </form>
    

    the above code is incorrect

    Can someone point out what the the If-condition should be??

    ie if male is selected a pop-up box should appear


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    You need to give your form a name and reference the radiobuttons off that. So if you called the form gender_frm then you would use
    if (gender_frm.sex.m.checked) 
    

    in the doit function. This works in IE 6, dunno about anything else.


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    Thanks

    does anyone know how to get something similar with netscape??


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Ok, there are two things. You need to reference variables properly. A bit like if you were looking for a folder on your computer. Just to be told, "Go to the Tools Folder" would be useless without a point of reference, i.e. "Go to the Tools Folder in the c:\windows\ directory" or "Go to the Tools Folder, one level above the folder you are currently in"

    So you must give a reference for the variables. There are a few ways of doing this.

    My favourite, because of doing Java, is to pass an object to the Javascript functions, so for example
    <script language="JavaScript">
    function doit(aForm) {
    	if (aForm.sex.m.checked) 
    		alert('male');
    	else if (aForm.sex.f.checked) 
    		alert('female');
    	else
    		alert('something wrong');
    }
    </script>
    <form name="myForm">
    <input type="radio" name="sex" id="m" value="male" checked> Male
    <br>
    <input type="radio" name="sex" id="f" value="female"> Female
    <br>
    <input type="button" value="Hello world!" onclick="doit(myForm)">
    </form>
    
    The other way to do it, and what seems to be the staple of Web Developers, is to reference the object directly, so..
    <script language="JavaScript">
    function doit() {
    	if (document.myForm.sex.m.checked) 
    		alert('male');
    	else if (document.myForm.sex.f.checked) 
    		alert('female');
    	else
    		alert('something wrong');
    }
    </script>
    <form name="myForm">
    <input type="radio" name="sex" id="m" value="male" checked> Male
    <br>
    <input type="radio" name="sex" id="f" value="female"> Female
    <br>
    <input type="button" value="Hello world!" onclick="doit()">
    </form>
    

    What way you choose is up to yourself.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    You could also pass in the value of one of the option buttons. I've used an index to reference the first button so the id attributes are no longer needed.
    <script language="JavaScript">
    function doit(male) {
    	if (male) 
    	{
    		alert("Male");
    	}
    	else if (!male) // not male
    	{
    		alert("Female");
    	}
    	else
    	{
    		alert('thats just not right');
    	}
    }
    </script>
    <form name="gender_select">
    <input type="radio" name="sex" value="male" checked="true"> Male
    <br>
    <input type="radio" name="sex" value="female"> Female
    <br>
    <input type="button" value="Hello world!" onclick=doit(document.gender_select.sex[0].checked)>
    </form>
    

    Again I've only tested this in IE6


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    function doit() {
    	if (document.kevin.elements["m"].checked) 
    		alert('male');
    	else if (document.kevin.elements["f"].checked)
    		alert('female');
    	else
    		alert('aggh');
    }
    </script>
    
    
    
    
    <form name="kevin">
    <input type="radio" name="sex" id="m" value="male" checked> Male
    <br>
    <input type="radio" name="sex" id="f" value="female"> Female
    <br>
    <input type="button" value="Hello world!" onclick=doit()>
    </form>
    

    works with netscape

    thanks
    everyone


  • Advertisement
Advertisement