1

I'm trying to make a function that changes the color of text when a checkbox is checked. It was working fine, but all of the sudden now it won't work It says that the toggle variable is an undefined expression, but it's just an id tag being stored in it. also, the css changes weren't working, even when the alert would pop up. I would greatly appreciate any info on why this function suddenly broke.

JavaScript, jQuery code:

function estimateToggle(toggle)
{ 
    var toggle = "#" + toggle;
    alert(toggle);

    if  ($(toggle).is(":checked"))
    {
        alert(toggle + " is checked");
        $("'" + toggle + "'").css({'color':'black'});
    }else{
        alert(toggle + " is unchecked");
        $("'" + toggle + "'").css({'color':'#CCCCCC'});
    }

}

HTML code:

<input type="checkbox" class="socialMedia" name="socMedFacebook" id="socMedFacebook" onChange="estimateToggle('socMedFacebook')" value="100">
<label for="socMedFacebook">Facebook</label>
<div align="right" class="socMedFacebook" id="socMedFacebook" name="socMedFacebook">$100</div>

Here is the error that Chrome Console spits out:

Uncaught Error: Syntax error, unrecognized expression: '#socMedFacebook'

2
  • You have 2 elements with the same ID. Invalid markup. maybe socMedFacebook-div and socMedFacebook-box ? Commented Oct 23, 2013 at 0:26
  • I changed it to this $(toggle + "1".css({ Annd it works now. TY so much for your guys' help. Commented Oct 23, 2013 at 0:32

1 Answer 1

2

ID selector must be unique .

Read Two HTML elements with same id attribute: How bad is it really?

change to

$(toggle).css({

form

 $("'" + toggle + "'") // as your variable toggle  is already string don't wrap quotes around it.
Sign up to request clarification or add additional context in comments.

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.