views:

368

answers:

7

I'm trying to use jquery to enable a form button only after a radio button has been selected. It's a t-shirt shop where the user is required to choose a size before the 'add to cart' button becomes enabled. The html looks a bit like this:

<input class="radioclick product21" type="radio" name="variation[variation_select_21_1]" value="1">Big
<input class="radioclick product21" type="radio" name="variation[variation_select_21_2]" value="2">Small

<input type="submit" id="product21" class="buy_button" name="Buy" disabled="disabled" value=" ">

So, the submit button is disabled. To enable it when the user clicks a radio button, I use this jquery:

$(function()
{
     $('.radioclick').click(function()
    {
        $('.buy_button').removeAttr('disabled');
        $('.buy_button').attr('value', 'add to cart');  
    });
});

However, that enables ALL the submit buttons on the page (there's lots of t-shirts on the page). The second class name of the radio button is the same as the id of the submit button, so is there a way to use this classname to target the enabling of the specific submit button?

(hope that all makes sense!)

A: 

You will need to give the radio buttons unique ids or at least add them to parent div with a unique id instead because $('.radioclick').click is going to execute every time a radio button of that class is clicked like you said.

$('#parentID').('.radioclick').click(...)

Assuming you split the different groups into sections with unique ids.

Jason Rikard
A: 

with the html you showed, next method comes to mind.
example:

$(function()
{
     $('.radioclick').click(function()
    {
        $(this).next('.buy_button').removeAttr('disabled');
        $(this).next('.buy_button').attr('value', 'add to cart');  
    });
});

If the markup is wrapped in a container (div or otherwise), you could get the id/class from the container to look for the submit button in the container:

$(function()
{
     $('.radioclick').click(function()
    {
        var containerClass = $(this).parent().attr("class");
        $('.buy_button', containerClass).removeAttr('disabled');
        $('.buy_button', containerClass).attr('value', 'add to cart');  
    });
});

take a look through the selectors. You can get pretty creative with them. http://docs.jquery.com/Selectors

Bless Yahu
Not sure, but your `.class()` is not a jQuery function. The function would work if you just removed the `.class()`, since `$( selector, scope )` works best with an existing jQuery set or a DOM object for the scope.
Doug Neiner
Your right. I should have wrote var containerClass = $(this).parent().attr("class");And I agree with your second statement, as long as you know the scope. That's what the above statement was trying to grab. The question example sets the click function for ANY .radioclick, so the scope might be different. That is why I'm trying to grab the parent container's class name.
Bless Yahu
+3  A: 

To answer your question exactly:

$(function() {
    $('.radioclick').click(function(){
        var id = this.className.split(' ')[1]; // second class
        $('#' + id).removeAttr('disabled').attr('value', 'add to cart');  
    });
});

But there are less invasive ways to do it, but it depends on your markup.

EDIT: Since you said each is in their own form, just use this code:

$(function() {
    $('.radioclick').click(function(){
        $(this).closest('form')
               .find('.buy_button')
               .removeAttr('disabled')
               .attr('value', 'add to cart');  
    });
});

This takes the radio button that was clicked, finds the parent form, then scopes the search for the .buy_button to that form only.

Doug Neiner
+1, very nice answers
Dan F
+1  A: 

Use a wrapper with div or p or table cell etc. My sample below uses div with a class "wrapper_<%=productid%>" in the ASPX.

Rendered HTML:

<div id="wrapper_product21">
<input class="radioclick product21" type="radio" name="variation[variation_select_21_1]" value="1">Big
<input class="radioclick product21" type="radio" name="variation[variation_select_21_2]" value="2">Small

<input type="submit" id="product21" class="buy_button" name="Buy" disabled="disabled" value=" ">
</div>

Rendered Javascript:

$(function()
{
     $('.radioclick').click(function()
    {
        var productButton = $('#' + "wrapper_" + $(this).id + ' .buy_button');
        productButton.removeAttr('disabled');
        productButton.attr('value', 'add to cart');  
    });
});
o.k.w
A: 

If you really like the idea of using that second class name to specify the id of the submit button you want to use you can do that (I'm not endorsing it, but if that's what you want to do... haha) , but you'll need to get goofy with looking at the classes.

This, of course is completely untested, lol... but give something like this a try (this assumes that 'product' will always be the start of the submit button's id):

$(document).ready(function() {
   $('.radioclick').click(function() {
      var classAttr = $(this).attr('class');
      var classes = classAttr.split(' ');
      var submitId;
      for(var i = 0; i < classes.length; i++) {
         var c = classes[i];
         if(c.indexOf('product') == 0) {
            submitId = c; 
         }
      }
      if(submitId) {
         $('#' + submitId).).removeAttr('disabled').attr('value', 'add to cart');  
      }
   });
});
blesh
lpfavreau's idea is the same, but better. I'd go with that one... haha.
blesh
+1  A: 

I'm not sure I get exactly what you want and hence I'm not sure it's the best way to do what you are trying to do... There might be something more elegant to try but here's a way to target the ids of the second classes 'productXY':

$('.radioclick').click(function() {
    var id = null;
    var classes = $(this).attr('class');
    if (id = classes.match(/product[0-9]*/)) {
        $('#' + id).removeAttr('disabled').attr('value', 'add to cart');              
    }
});
lpfavreau
I like this better than my answer, but I'd consolidate the last few lines: $('#'+id).removeAttr('disabled').attr('value','add to cart');Also I'd add a check to var classes before I got var id to make sure it wasn't null or empty.
blesh
Thanks blesh, good comments.
lpfavreau
+1  A: 

Based on the element hierarchy you have provided, the easiest is to check the siblings for an element with a class buy_button and change it:

$(function()
{
     $('.radioclick').click(function()
    {
        $(this).siblings('.buy_button')
               .removeAttr('disabled')
               .attr('value', 'add to cart');

    });
});
prime_number