0

I'm hoping this is something silly I've done. I got a function unigref near the bottom, which (I believe) outputs a string. However, when I call the function to build a jQuery selector, I can't get it to work properly. I know that everything else works because when I use a static string the radio button is selected.

Here's my jsfiddle/9Edxx. Please help.

var checkCount = 0;
var maxChecks = 2;

$(document).ready(function() {

$("#test").click(function() {
    alert($(':checked').length);
});

$(':checkbox[name=checkbox]').change(function() {


    checkCount = $(':checkbox:checked').length;

    if (checkCount >= maxChecks) {
      $(':checkbox[name=checkbox]').not(':checked').attr('disabled', true);  
        $(":radio[value="+uniqref()+"]").prop('checked', true);


    } else {
      $(':checkbox[name=checkbox]:disabled').attr('disabled', false);
    }

    if (this.checked) {
        $("td.label").append("<label>" + this.value + "</label>");
    } else {
        $("td.label").find(":contains('" + this.value + "')").remove();
    }

});

$('#button').click(function() {
    alert(uniqref());
});


function uniqref() {
    return $("td.label").text().split('').sort().join('').replace(/\s/g, "");
}


});​

UPDATE: The typo has been correct, but the problem still exists.

http://jsfiddle.net/9Edxx/

0

3 Answers 3

9

Yeah it's really silly: It's just a typo.

$(":radio[value="+unigref()+"]").prop('checked', true);

should be

$(":radio[value="+uniqref()+"]").prop('checked', true);

with a lowercased Q instead of a G.


Also, you're calling uniqref() before actually updating the value of td.label.

Should be in this order:

if (this.checked) {            
    // ...
}

if (checkCount >= maxChecks) {            
    // ...
}

http://jsfiddle.net/7mvmT/7/

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

1 Comment

I hoped that was it but alas the correcting the typo still does not solve the problem :S
4

http://jsfiddle.net/7mvmT/6/

Basically this line:

$(":radio[value="+uniqref()+"]").prop('checked', true);

is called prematurely (before the checkbox is actually checked. A simple, ugly hack:

setTimeout(function(next) {               
     $(":radio[value="+uniqref()+"]").prop('checked', true);    
}, 0);

solves it.

Also you had a typo as Niko mentioned.

2 Comments

Thank you! I've spent the whole weekend doing this! I'll mark you as the correct answer. Thanks!! (Feel bad for Niko, is there anyway I can appreciate his input?)
@SajeevShanmuganandarajah Don't worry about that, you're supposed to select the answer that provides a solution for the major fault ;-)
1

No need for hack. http://jsfiddle.net/dn7gM/

p.s.: only works for the 2 first radios, since not all ids are setted correctly ;-)

1 Comment

definitely a more elegant solution

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.