1

I'm working on a codecademy.com lesson that's teaching me how to build a bar graph with jQuery. The last set of instructions are to add a click event to $bar to display it's value. It explains

Anonymous functions go like this: function(arg) { code }. In this case, a .click handler will have e, the click event, as its argument, so your implementation should have function(e) { alert code } within the .click().

Here's the function I'm supposed to modify

// Exercise 5
function addBarClickEvent($bar,value) {
    // add a click event to the bar that 
    // pops up an alert with the bars value
    $bar.click(
        // your code goes here!

        }
    );
}

I tried both of the solutions shown below but neither worked. The lesson doesn't provide a solution unfortunately. Can anyone help.

// Exercise 5
    function addBarClickEvent($bar,value) {
        // add a click event to the bar that 
        // pops up an alert with the bars value
        $bar.click(
            // your code goes here!
              alert($bar.value)       //my attempt
              alert(e.value)          //my second attempt
            }
        );
    }

I'm getting this error message

Make sure to define the argument to .click as an anonymous function that displays the alert.

2
  • @robbrit didn't work. I'm getting this error message "Make sure to define the argument to .click as an anonymous function that displays the alert." Commented Sep 5, 2012 at 18:03
  • @user1647484 look at Robin Maben's answer. You forgot the function(){ part Commented Sep 5, 2012 at 18:04

2 Answers 2

2

Assuming the value you want to display is the value that was passed at the time of binding

function addBarClickEvent($bar,value) {
    // add a click event to the bar that 
    // pops up an alert with the bars value
    $bar.click(function(){
       alert(value);
    });
}

If not -

$bar.click(function(){
   // get value somehow
  //$(this).data('some-value');
});
Sign up to request clarification or add additional context in comments.

3 Comments

doesn't work. by the way, I'm getting this error message for all the attempts i've made. "Make sure to define the argument to .click as an anonymous function that displays the alert."
@user1647484: It is an anonymous function in both cases.
Maybe it's erroring on the missing e argument? The error message received certainly isn't a built-in javascript error, so I'm guessing the anonymous function needs to be constructed according to the instructions in order to work.
0

The question tells you exactly what to do: "your implementation should have function(e) { alert code } within the .click()"

Just write what it says: $bar.click(function(e) { alert(this.value); }

Adjust the this.value depending on where the value actually is - I don't know and can't guess ;)

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.