jquery - Drop down menu to display and hide input fields -


I am trying to create a form where a user selects his preferred method of contact and only for that method Required / visible, therefore the email and phone input fields are hidden initially, when they select a phone or email, then only that field is displayed below.

& lt; Script & gt; $ ('# Contact-method') Change (function () {var selected_item = $ (this) .val () if (select_item == "email") {$ ('# email'). Val ("") .removeClass ('hidden'); $ ("#"). Val (""). RemoveClass ('required');} if (selected_tym == "phone") {$ ('# phone'). Val ("") .removeClass ('hidden' ); $ ('# Email'). Val (""). RemoveClass ('Required');} and {$ ('# Phone'). Val (selected_item) .addClass 'hidden'); $ ('# Email'). Val (selected_item) .addClass ('hidden');}}); & Lt; / Script & gt; & Lt; Label & gt; Contact's preferred method? & Lt; / Labels & gt; & Lt; Select name = "contact-method" class = "selection field" id = "contact-method" & gt; & Lt; Option value = "" & gt; Please select ... & lt; / Options & gt; & Lt; Option value = "phone" & gt; Phone & lt; / Options & gt; & Lt; Option value = "email" & gt; E-mail & lt; / Options & gt; & Lt; / Select & gt; & Lt; Input type = 'text' id = "phone" placeholder = "phone" class = "hidden required" /> & Lt; Input type = 'text' id = "email" placeholder = "e-mail" class = "hidden required" /> & Lt; Input name = "contact" type = "submit" class = "submit" id = "contactus" value = "submit" />

You made some mistakes in your javascript, below I have posted correctly JS

First you have forgotten to add else after the second if statement; secondly, you have to add the selected class to the non-selected option that it One option will hide while showing the other.

  $ ('# contact-method'). Change (function () {var selected_item = $ (this) .val () if (Val ("#"). $ ('# Email'). Val (""). RemoveClass ('hidden'); $ (' Val (selected_item) .addClass ('hidden'); $ ('#phone'). Val (""). RemoveClass ('required');} and if (selected_item == "phone") { $ ('# Phone'). Val (""). RemoveClass ('' hidden '' ('' '' '). $ (' # Email ') .le (selected_tem) .addClass (' hidden '); $ ('# Email'). Val (""). RemoveClass ('required');} and {$ ('Val (selected_item) .addClass (' hidden '); $ (' # email '). Val (Selected_item) ) AddClass ('hidden');}});  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -