0

Can someone tell me what is wrong with this javascript ?

      $("#parent-project").attr("data-id").click(function(){ 
       //some code...
      }

The console output is:

Cannot read property 'click' of undefined

4
  • Sure, .attr("data-id") returns a string, which has no click method Commented Jun 15, 2015 at 16:39
  • how can I solve this issue ? Because the only way to select the right div is by data-id attribute Commented Jun 15, 2015 at 16:40
  • @NathanSchwarz, in my answer an element with id parent-project and attribute data-id is selected Commented Jun 15, 2015 at 16:42
  • $("#parent-project[data-id="+myVar+"]").click(function(){ // code } Commented Jun 15, 2015 at 16:42

1 Answer 1

4

Using attr() will return a string value to which a click handler cannot be attached. I imagine you wanted

$("#parent-project[data-id]").click(function(){ 
       //some code...
      }

In which [] is notation for attribute

Sign up to request clarification or add additional context in comments.

5 Comments

I've got an other question. Is it possible to compare only the data-id of different elements ?
@NathanSchwarz, what do mean compare? Can you give me some HTML and what you want so I can better understand?
I would like to compare the data-id to get something like :if ($("#parent-project[data-id]") != ("#open_project[data-id]")) { but only compare the data-id
@NathanSchwarz, like this if ($("#parent-project").attr("data-id") != $("#open_project").attr("[data-id]"))
@NathanSchwarz, there was typo in the comment above, I fixed it

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.