0

I'm trying to debug and I keep getting a syntax error. I'm not sure what is going on with this. I'm trying to have it where if I click on an object which is attached to "$color_cell" it pushes the atrr("id") into the black_colors array.

Could someone help me with this? Thanks in advance.

<script src="jquery-1.6.2.js"></script>
<script type="text/javascript">

var ids = [];
var black_colors = [];
var blue_colors = [];
var brown_colors = [];
var gray_colors = [];
var green_colors = [];
var orange_colors = [];
var pink_colors = [];
var purple_colors = [];
var red_colors = [];
var teal_colors = [];
var white_colors = [];
var yellow_colors = [];


$(document).ready(function($) {


    $(".btnColor").click(function(){

        $(".color_cell").click(function(){

            if $(this).attr("id") == "black"{

            black_colors.push($(this).attr("id"));

    }

        });

    });

});

</script>
<br>
<br>
    <button type="button" class="btnColor" id="black">Black</button>&nbsp;
    <button type="button" class="btnColor" id="blue">Blue</button>&nbsp;
    <button type="button" class="btnColor" id="brown">Brown</button>&nbsp;
    <button type="button" class="btnColor" id="gray">Gray</button>&nbsp;
    <button type="button" class="btnColor" id="green">Green</button>&nbsp;
    <button type="button" class="btnColor" id="orange">Orange</button>&nbsp;
    <button type="button" class="btnColor" id="pink">Pink</button>&nbsp;
    <button type="button" class="btnColor" id="purple">Purple</button>&nbsp;
    <button type="button" class="btnColor" id="red">Red</button>&nbsp;
    <button type="button" class="btnColor" id="teal">Teal</button>&nbsp;
    <button type="button" class="btnColor" id="white">White</button>&nbsp;
    <button type="button" class="btnColor" id="yellow">Yellow</button>&nbsp;

3 Answers 3

3

instead of : if $(this).attr("id") == "black"

if ($(this).attr("id") == "black")
Sign up to request clarification or add additional context in comments.

Comments

1

There are many errors in your code.

<script src="jquery-1.6.2.js"></script>

should become:

<script type="text/javascript" src="jquery-1.6.2.js"></script>

and:

$(document).ready(function($) {

should become:

$(document).ready(function() {

and:

if $(this).attr("id") == "black"{

should become:

if ($(this).attr("id") == "black") {

And also you have nested the two .click() handlers which is not correct.

so that finally you have:

<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript">

var ids = [];
var black_colors = [];
var blue_colors = [];
var brown_colors = [];
var gray_colors = [];
var green_colors = [];
var orange_colors = [];
var pink_colors = [];
var purple_colors = [];
var red_colors = [];
var teal_colors = [];
var white_colors = [];
var yellow_colors = [];

$(document).ready(function() {
    $(".color_cell").click(function() {
        // Can't see any color_cell class in your markup
        // so no idea what you want to do if the DOM element with it is clicked
    });

    $(".btnColor").click(function() {
        if ($(this).attr("id") == "black") {
            black_colors.push($(this).attr("id"));
        }
    });
});
</script>

1 Comment

Only the last one is a real error. Default scripting language in HTML5 is JavaScript (ok, we don't know if it is HTML5) and the jQuery passes a reference to itself as first argument to the ready handler.
1

I don't think your javascript syntax is quite right. Try this:

$(document).ready(function($) {
    $(".btnColor").click(function(){
        $(".color_cell").click(function(){
            if ($(this).attr("id") == "black") {
               black_colors.push($(this).attr("id"));
             }
        });
    });
});

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.