2

I have two forms and a selector.

This is my code --

<select>
<option value="1">Pay</option>
<option value="2">Goog</option>
</select>

<form id="pp">
<input type="text">
</form>

<form id="cc">
<input type="text">
</form>

Now if option 1 is selected i want to hide form CC. if 2 hide form PP.

How do i do it with js or jquery? thanks

1

2 Answers 2

8

Try this (using jQuery):

$("select").bind("change", function() {
    if ($(this).val() == "1") {
        $("#pp").show();
        $("#cc").hide();
    }
    else if ($(this).val() == "2") {
        $("#pp").hide();
        $("#cc").show();
    }
});

Additionally, you could hide both forms using .hide() as shown above before the user selects any option.

  • bind is attaching an event handler to the "change" event of the select box. This is fired when the user changes what option is selected.
  • Inside the handler, val is used to determine the value of the currently selected option.
  • show() and hide() are used on the correct forms, depending on which option was selected.

Working example: http://jsfiddle.net/andrewwhitaker/faqZg/

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

Comments

1
    <script>
    function Hide(val)
    {
    if(val==1)
{
    document.getElementById('cc').style.display='none';
    document.getElementById('pp').style.display='inline';
    }
    if(val==2)
{ 
   document.getElementById('pp').style.display='none';
    document.getElementById('cc').style.display='inline';
    }
} 
   </script>

    <select onchange="Hide(this.value);">
    <option value="">Please Select</option>
    <option value="1">Pay</option>
    <option value="2">Goog</option>
    </select>

    <div id="pp">
    <input type="text">
    </div>

    <div id="cc">
    <input type="text">
    </div>

3 Comments

this is plain javascript, OP would prefer a jquery based answer I guess
@ngen says you have a spelling mistake if(val==1){document.getElementById('cc').style.display='none';document.getElementById('pp').style.display='inlinr';} - should be document.getElementById('pp').style.display='inline';
and not even very good code.. <script>.. which type? you should add type="text/javascript" - and properly indent your code

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.