tags:

views:

2479

answers:

5

I'm trying to disable these radio buttons when a the loadActive link is clicked but for some reason it only disables the first in the order and then skips the rest.

<form id="chatTickets" method="post" action="/admin/index.cfm/">
    <input id="ticketID1" type="radio" checked="checked" value="myvalue1" name="ticketID"/>
    <input id="ticketID2" type="radio" checked="checked" value="myvalue2" name="ticketID"/>
</form>
<a href="#" title="Load ActiveChat" id="loadActive">Load Active</a>

And Here is the jquery i'm using:

jQuery("#loadActive").click(function() {
    //I have other code in here that runs before this function call
    writeData();
});
function writeData() {
    jQuery("input[name='ticketID']").each(function(i) {
 jQuery(this).attr('disabled', 'disabled');
});
}
+2  A: 

First, the valid syntax is

jQuery("input[name=ticketID]")

second, have you tried:

jQuery(":radio")

instead?

third, why not assign a class to all the radio buttons, and select them by class?

mkoryak
Actually "input[name='ticketID']" is valid syntax, the single quotes are just not necessary.
karim79
why not just use the name? and select them by that? it should work fine; i've done it a million times.
geowa4
I agree with mkoryak: "assign a class to all the radio buttons, and select them by class"
Floetic
then you have to maintain both classnames and names when names suffice
geowa4
jQuery selector speeds - classes are faster than attributes
gnarf
i think we should solve his problem before making optimizations.
geowa4
A: 

You should use classes to make it more simple, instead of the attribute name, or any other jQuery selector.

rogeriopvl
let me guess, you use rails?
geowa4
Actually I don't. Can you elaborate?
rogeriopvl
rails has that array notation stuff for checkboxes. but you can have multiple two items with the same name without "[]"
geowa4
Yes you're right... I forgot completely about that. Going to edit the post.
rogeriopvl
+3  A: 

I've refactored your code a bit, this should work:

jQuery("#loadActive").click(writeData);

function writeData() {
    jQuery("#chatTickets input:radio").attr('disabled',true);
}

If there are more than two radio buttons on your form, you'll have to modify the selector, for example, you can use the starts with attribute filter to pick out the radios whose ID starts with ticketID:

function writeData() {
    jQuery("#chatTickets input[id^=ticketID]:radio").attr('disabled',true);
}
karim79
that'll disable every radio button in that form. in his question there might only be two, but there are likely going to be more.
geowa4
@geowa4 - true, I assumed those were the only radios on his form. In any case, I've edited to be a bit more useful.
karim79
+1 for removing the useless `.each()`; `.attr()` will apply to all selected elements anyway.
gnarf
+1  A: 

I just built a sandbox environment with your code and it worked for me. Here is what I used:

<html>
  <head>
    <title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
  </head>
  <body>
    <form id="chatTickets" method="post" action="/admin/index.cfm/">
      <input id="ticketID1" type="radio" checked="checked" value="myvalue1" name="ticketID"/>
      <input id="ticketID2" type="radio" checked="checked" value="myvalue2" name="ticketID"/>
    </form>
    <a href="#" title="Load ActiveChat" id="loadActive">Load Active</a>

    <script>
      jQuery("#loadActive").click(function() {
        //I have other code in here that runs before this function call
        writeData();
      });
      function writeData() {
        jQuery("input[name='ticketID']").each(function(i) {
            jQuery(this).attr('disabled', 'disabled');
        });
      }
    </script>
  </body>
</html>

I tested in FF3.5, moving to IE8 now. And it works fine in IE8 too. What browser are you using?

geowa4
A: 

code:

function writeData() {
    jQuery("#chatTickets input:radio[id^=ticketID]:first").attr('disabled', true);
    return false;
}

See also: Selector/radio, Selector/attributeStartsWith, Selector/first

ranonE