1

I am trying to make a Yes/No input change the function of a button. If the input is Yes then I want the button to be a hyper link, but if it is a no I want it to take you to the next div (once the button is clicked)

I have NO idea how to do this.

Here is what I have

I am imagining I need a jquery thing to start like this:

$('input:radio[name="yes"]').change(
function(){
    if ($(this).val() == 'Yes') {
        $('.1').css('display', 'block');
    }
    else {
        $('.2').css('display', 'block');
    }
});

I tried giving the buttons a different class and then trying to have the show up, but that wasn't working.

<form>
    <input type="radio" name="yes" value="Yes"/>Yes
<input type="radio" name="yes" value="No"/>No
</form>

<a href="#"><button>Yes</button></a>
<button class="no">No</button>

This is what the buttons would look like

Any help would be great!

1 Answer 1

2

You should try with this:

FIDDLE

HTML:

 <input type="radio" name="button" value="Yes" />Yes
 <input type="radio" name="button" value="No" />No

 <button class='yes'>Yes</button>
 <button class="no">No</button>

jQuery:

 $('input:radio').change(function () {
   if ($(this).val() == 'Yes') {
      $('[class="yes"]').show().siblings('button').hide();
   } else {
      $('[class="no"]').show().siblings('button').hide();
   }
});

Try to group them with same name and try a change event on this radio button then show the corresponding button.

and even a better one:

$('input:radio').change(function () {
  var rad = this.value.toLowerCase();
  $('[class="' + rad + '"]').show().siblings('button').hide();
});

fiddle

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

6 Comments

Awesome! I was close on my idea lol Ok so when you load the page you get the Yes and No Button to show then when you click Yes or No the other dissapears. How do I make it so that only 1 button is visible the whole time, but button's function changes when the Yes or no is shown?
I think I figured it out. I can just set the CSS of the "no" to be display:none I think that will work
Absolutely doing that will do as per your requirement.
I also came up with another idea, which was to set one of the inputs to selected! I like this option better. Thanks again for the help!
out of curiosity why is the class=no in "[]"?
|

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.