0

Hi I have a small question.

I have 2 select form , and i want when i choose rental the employed type become hidden and here's my code :

<label>Type</label>
 <select id="Type-option" class="form-control">
     <option value="employed">Employed</option>
     <option value="rental">Rental</option>
</select>

<label>Employed Type</label>
<select id="employedType-option" value="employedType-option" class="form-control">
     <option value="fulltime">FULLTIME</option>
     <option value="parttime">PARTTIME</option>
</select>

$(function() {
 var select = $('#Type-option');
select.on('change', function() {
  if (select.val() == "rental") {
    $('#employedType-option').hide();
  } else {
    $('#employedType-option').show();
  }
 });
});

any idea ?

4
  • It is working properly. I don't get any issue. Check this- jsfiddle.net/4vopp97z Commented Oct 21, 2016 at 4:52
  • What's the error? I got your code running here: jsfiddle.net/p2tz02qf Commented Oct 21, 2016 at 4:52
  • @hi, its not working here in my local. nothing error. any idea ? Commented Oct 21, 2016 at 4:56
  • ur above code is working, check properly Commented Oct 21, 2016 at 5:01

4 Answers 4

1

Few suggestions here 1. Never mix your mark up with your javascript.Consider binding events at javascript 2. I have modified your markup a bit,because when you hide employeetype u need to hide the label too

check the following snippet

$(document).ready(function(){
  var select = $('#Type-option');
  var employeeTypeOption=$('#employedType-option');
  var employedType=$('#employedType');
   select.on('change', function() {
    var selectOption=select.find('option:selected').val();
  if (selectOption == "rental") {
   employeeTypeOption.hide();
  employedType.hide();
  } else {
    employeeTypeOption.show();
    employedType.show();
  }
 });
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Type</label>
 <select id="Type-option" class="form-control">
     <option value="employed">Employed</option>
     <option value="rental">Rental</option>
</select>

<span id="employedType">
<label>Employed Type</label>
<select id="employedType-option" value="employedType-option" class="form-control">
     <option value="fulltime">FULLTIME</option>
     <option value="parttime">PARTTIME</option>
</select>
</span>

Hope this helps

Sign up to request clarification or add additional context in comments.

5 Comments

cool...If it is of any help,please consider accepting answer :)
why I add condition like this, not working. else if (select.val() == "employed" || "parttime"){ $('#employedType-option').show(); $('#employedType').show(); $('#basic-salary').hide(); $('#hourly-rate').show();
why I add condition like this, not working. else if (select.val() == "employed" || "parttime"){ $('#employedType-option').show(); $('#employedType').show(); $('#basic-salary').hide(); $('#hourly-rate').show();
parttime is of employedType-option box.you cannot check it on select.val() .change the if condition to ( selectOption=="employed") || $("#employedType-option option:selected").val()=="parttime") .but a question here ,by default employed dropdown would be visible. When it is hidden by previous if condition how can check if it is parttime or not
the issue is to long too explain it in here. can we have a small chit chat ?
0

wrong syntax. check $(document).on.('action','selection',function(){}); signature.

Tyr with below code

 $(document).on('change','#Type-option', function() {
 if ($(this).val() == "rental") {
    $('#employedType-option').hide();
 } else {
    $('#employedType-option').show();
 }
 });

Comments

0

I am not sure but i guess you have missed one of these otherwise your code is working fine

  • Add reference to jquery library
  • Wrap your jquery code inside script tag.

    <script>
     $(function() {
     var select = $('#Type-option');    select.on('change', function() {
     if (select.val() == "rental") {
       $('#employedType-option').hide();
     } 
     else {
       $('#employedType-option').show();
     }
      });    
    });
    
    </script>
    

2 Comments

it's inside script Wrap. and I've added bootstrap jquery.
See your console you must be getting $ is not defined
0

I think this is what you want. try the working demo

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>


<label>Type</label>
 <select id="Type-option" class="form-control">
     <option value="employed">Employed</option>
     <option value="rental">Rental</option>
</select>


<label id="emp">Employed Type</label>
<select id="employedType-option" value="employedType-option" class="form-control">
	 <option value="fulltime">FULLTIME</option>
	 <option value="parttime">PARTTIME</option>
</select>



<script type="text/javascript">
	
	$("#Type-option").on('change',function(){
		var theVal = $(this).val();//get the selected value

		if(theVal == "rental"){
			$("#emp").hide();
			$("#employedType-option").hide()
		}
		else{
			$("#emp").show();
			$("#employedType-option").show()
		}

	})

</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.