1

I have two two <p> and one <div> and want event handler to display a message when the first <p> is pressed. How can I pull that off?

$("#menu:first-child").on('click', 'p', pullInput);

function pullInput() {
  console.log("da");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menu">
  <p id="newCategoryButton">New category</p>
  <p>New Song</p>
</div>

4
  • 2
    why not use ID newCategoryButton?$("#menu").on('click','#newCategoryButton',pullInput);? Commented Jan 9, 2017 at 6:51
  • 1
    Why the event delegation? Commented Jan 9, 2017 at 6:53
  • @guradio is right. Or even more simple - $("#newCategoryButton").on("click", pullInput ); Commented Jan 9, 2017 at 6:54
  • The paragraphs elements have opacity 0 first and when I press a button they have opacity 1,maiby they act like dinamicaly created elements because none of the examples are working.The only one is this "$("#menu").on('click','p',pullInput);" but it gets both of the paragraphs Commented Jan 9, 2017 at 6:59

2 Answers 2

4

Your selector is wrong. Update your selector like this

$("#menu p:first-child").on('click', pullInput);
function pullInput(){
  console.log("HI!");
}
     
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menu">
  <p id="newCategoryButton">New category</p>
  <p>New Song</p>
</div>

Working fiddle https://jsfiddle.net/9amv3vmh/

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

2 Comments

The paragraphs elements have opacity 0 first and when I press a button they have opacity 1,maiby they act like dinamicaly created elements because none of the examples are working.The only one is this "$("#menu").on('click','p',pullInput);" but it gets both of the paragraphs
Can you show us the fiddle... i mean including the button, on clicking which opacity changes
1

You need to use :first-child selector with p element.

$("#menu").on('click','p:first-child',pullInput);

$("#menu").on('click', 'p:first-child', pullInput);

function pullInput() {
  console.log("da");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menu">
  <p id="newCategoryButton">New category</p>
  <p>New Song</p>
</div>


However as you have sepecified ID with p element use it to bind event handler.

$("#newCategoryButton").on('click', pullInput);

2 Comments

The paragraphs elements have opacity 0 first and when I press a button they have opacity 1,maiby they act like dinamicaly created elements because none of the examples are working.The only one is this "$("#menu").on('click','p',pullInput);" but it gets both of the paragraphs
@S.Georgian, Can you create Fiddle or snippet to show the issue?

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.