SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question value on radio buttons

    hi, i'm a newbie in javascript..
    recently, i'm trying to do some form validation in javascript...
    however, there are some difficulties in radio buttons.


    Code:
    <script type="text/javascript">
    function validate()
    {
    
    	if(document.getElementById('interest').value == "2")
    	{
    		if(document.getElementById('sport').value=="")
    		{
    		alert("not ok")
    		}
    	}
    	
    	if(document.getElementById('interest').value == "1")
    	{
    		document.getElementById('sport').disabled=="true")
    		
    	}
    	
    }
    
    </script>
    
    
    <input type="radio" id="interest" name="interest" value="1">Reading
    <input type="radio" id="interest" name="interest" value="2">Sport
    
    <select name="sport" id="sport">
    <option value=""></option>
    <option value="1">Swimming</option>
    <option value="2">Gym</option>
    
    
    </select>
    
    <input type="submit" value="submit" onclick="validate()">
    I wish to make, if user selected Sport at the radio button, then he will need to make a selection at the Sport field. Else, if the user selected Reading at the radio button, the select Sport field is disable...

    how can i make it works?

    thank you!

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    function validate()
    {
    var el = document.getElementById('interest1');
    var el2 = document.getElementById('interest2')
    var sel = document.getElementById('sport')
    
    if(el.checked) { 	sel.disabled= true ; 	}
    if(el2.checked) { 	sel.disabled= false ; 	}	
    
    }
    </script>
    
    <form>
    <input type="radio" id="interest1" name="interest" value="1" onclick="validate()">Reading
    <input type="radio" id="interest2" name="interest" value="2" onclick="validate()">Sport
    
    <select name="sport" id="sport" disabled>
    <option value=""></option>
    <option value="1">Swimming</option>
    <option value="2">Gym</option>
    </select>
    
    <input type="submit" value="submit" onclick="validate()">
    </form>
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by muazzez View Post
    Code:
      
    <script type="text/javascript">
    function validate()
    {
    var el = document.getElementById('interest1');
    var el2 = document.getElementById('interest2')
    var sel = document.getElementById('sport')
    
    if(el.checked) { 	sel.disabled= true ; 	}
    if(el2.checked) { 	sel.disabled= false ; 	}	
    
    }
    </script>
    
    <form>
    <input type="radio" id="interest1" name="interest" value="1" onclick="validate()">Reading
    <input type="radio" id="interest2" name="interest" value="2" onclick="validate()">Sport
    
    <select name="sport" id="sport" disabled>
    <option value=""></option>
    <option value="1">Swimming</option>
    <option value="2">Gym</option>
    </select>
    
    <input type="submit" value="submit" onclick="validate()">
    </form>
    oh... this is the correct way of doing it...

    thanks a lot muazzez...


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
  •