0

I am new to javascript and jQuery. I am trying to add an attribute on click and remove it on click. I have add and remove class working well but not attribute. My code is below.

HTML below

<label class="label_check" for="fadv">
    <input class="lesson" id="fadv" value="acl323" name="fadv" checked="checked" type="checkbox" />Please send me regular updates.
</label>

jQuery below

<script>
function setupLabel() {
    if ($('.label_check input').length) {
        $('.label_check').each(function(){ 
            $(this).removeClass('c_on');
            $(this).removeAttr('pull');
        });
        $('.label_check input:checked').each(function(){ 
            $(this).parent('label').addClass('c_on');
            $(this).attr('pull' , 'on');
        });                
    };
};
$(document).ready(function(){
    $('body').addClass('has-js');
    $('.label_check, .label_radio').click(function(){
        setupLabel();
    });
    setupLabel();
});
</script>
2
  • why do you want to add an attribute ? Commented May 18, 2012 at 11:28
  • Could you explain what you want to happen on clicking the checkbox? Or is it the label that should be the click-target? Commented May 18, 2012 at 11:53

2 Answers 2

1

Can you empty the attribute value?

function setupLabel() {
    if ($('.label_check input').length) {
        $('.label_check').each(function(){ 
            $(this).removeClass('c_on');
            $(this).attr('pull','');
        });
        $('.label_check input:checked').each(function(){ 
            $(this).parent('label').addClass('c_on');
            $(this).attr('pull' , 'on');
        });                
    };
};
$(document).ready(function(){
    $('body').addClass('has-js');
    $('.label_check, .label_radio').click(setupLabel);
    setupLabel();
});
Sign up to request clarification or add additional context in comments.

1 Comment

function(){ setupLabel(); } could be simplified as setupLabel
0

because label_check doesnt have this attribute :)) could u try this code ?

http://jsfiddle.net/Nzw2K/

Comments

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.