0

I have in one form, one selects with one question that if determined value selected I need to display none the next question. How can I make a jquery code that detects that the user selected a determinated option in select and change the CSS of the page?

<select>
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="mercedes">Mercedes</option>
   <option value="audi">Audi</option>
</select>
4
  • 5
    Show what you've got and we'll see what you need/ might improve on Commented Feb 1, 2019 at 10:43
  • This might be of some use to you stackoverflow.com/questions/32325864/… Commented Feb 1, 2019 at 10:48
  • @jamesS for now, i just have the form structure, i edited the question with an example. Commented Feb 1, 2019 at 10:52
  • @Rizwan ive added a jsfiddle to my answer, hope this helps. Commented Feb 1, 2019 at 11:49

3 Answers 3

2

So as far as I understand with your short description you need to do something based on the value selected by user in select box which we call an event onchange.I am writing a code example for you

<select onchange="somefunction($(this).val())">
 <option value="1">Option 1</option>
 <option value="2">Option 2</option>
 </select>

<script>
  function somefunction(value){
     if(value == 1){
      $("body").css("background-color","red");
     }
     if(value == 2){
       $("body").css("background-color","yellow");
     }
  }
   </script>
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please make a code with all in javascript code? withou puting "onchange="somefunction($(this).val())" in select tag.
$("#car_manufacturer").onchange(function(){ }); you can also do like this
yes it can work. add a body tag to your jsfiddle and you will see the effect
I don't use jsfiddle but on my IDE i have tested. its working fine
@Rizwan it works fine in my IDE, but when i put it on my site i got this error: Uncaught ReferenceError: somefunction is not defined at HTMLSelectElement.onchange ((index):2473) onchange @ (index):2473 can you make this code all in the script?
2

Your question is a bit vague, but i think this is what your after:

 const value = $('#selectID option[value=""]').prop("selected", true);

 if (value....") {
     // do css stuff
 }

Additionally you can get the value / text like so:

<select id="car_manufacturer">
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="mercedes">Mercedes</option>
   <option value="audi">Audi</option>
</select>

<script>
   const element = document.getElementById("car_manufacturer");
   const value = element.options[element.selectedIndex].value;
   const text = element.options[element.selectedIndex].text;
 </script>

You could extend on the above with a on onchange event on the select element.

<select id="car_manufacturer" onchange="selectManufacturer(this)">

Manipulate CSS:

JavaScript:

document.body.style.backgroundColor = "red";

jQuery:

$("body").css("background-color","red");

Fiddle

onchange example with css manipulation:

http://jsfiddle.net/9L0czmeq/

4 Comments

Nice, but i need to change css when select change value.
very nice @David, how can i use a class instead of body to change the css here: document.body.style.display = "none";??
jQuery - .addClass $( "#id" ).addClass( "class" ); there is also a .removeClass
JavaScript - var element = document.getElementById("div"); element.classList.add("class");
0

Pure css solution would be something like this:

.container:has(select[name="name"] option[value="value"]:checked) .selector-in-container {background:blue}

Or specifically something next to the select:

select[name="name"]:has(option[value="value"]:checked) + .element-after-select {background:blue}

Should work reliably across all Browsers.

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.