0

I need to find a checkbox with a class that is contained within the variable preciseClass.

My attempt below is not working. Where am I going wrong?

var preciseClass = $('#box2').attr('class');


$('#box1').find('li input:checkbox.' + preciseClass).attr('checked', 'checked');
3
  • space is missing after checkbox it should be checkbox .' Commented Jan 6, 2012 at 13:16
  • Use the javascript console in Firefox or Chrome. You can execute statements to verify that your selectors are correct. Commented Jan 6, 2012 at 13:17
  • @AmarPalsapure unless the input is what he's after. And considering input nodes don't usually have children, I'm guessing this is unlikely. Commented Jan 6, 2012 at 13:18

3 Answers 3

1

Could be that the node has multiple classes in which case you might be able to use something like this:

var preciseClass = $('#box2').attr('class').split(" ").join(".");

$('#box1').find('li input:checkbox.' + preciseClass).attr('checked', 'checked');
Sign up to request clarification or add additional context in comments.

2 Comments

@CecilTheodore could it be that you are using #box2 but looking under #box1?
Ignore that last comment, today has been a BAD day! thanks for your help youve helped me work it out.
1

It will depend on the value of preciseClass; if you have multiple class applied to box2 element : it will not work as you expect.

can you make an alert or a console.log of the value? http://jsfiddle.net/bxeys/

Comments

0

I think you may need a space after :checkbox

$('#box1').find('li input:checkbox .' + preciseClass).attr('checked', 'checked');

1 Comment

no... the . classname will cause it to look for any classname nodes, which probably don't exist.

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.