0

I am preparing for MS exam 70-480 and practicing JavaScript - made an test example with different variations of .click events, but it none are working (buttons are not applying styles not at all).

What is missing/wrong is this code? https://jsfiddle.net/svguetq7/

 // part of code
 $("#btn3").click(function(){
   $("article:first-of-type") .css("background-color", "blue");
 });

Update

Embaring moment - all buttons had identical IDs, so that was a problem too. Message to others - please check your code extra one more time before asking something to StackOverflow :)

4
  • Add the html. I bet you are missing a # or a . before article... Commented Oct 21, 2017 at 16:52
  • @Juan - can you update that example and post link? Because i did not understand clearly where you minded to put 'html'. Commented Oct 21, 2017 at 16:53
  • Inspect the page and check the console. Check the error there. Commented Oct 21, 2017 at 16:55
  • Great - missing jQuery. Thank you. :) Commented Oct 21, 2017 at 16:57

1 Answer 1

3

First, you have not included jQuery in the fiddle. Second, you are using the selector in a wrong way. If you need to select the first,second,third elements then use eq() function of jQuery. Sample code:

  $("#btn1").click(function() {
    $("section article:eq(0)").css("background-color", "red");
  });

Demo for the first element: https://jsfiddle.net/lotusgodkk/svguetq7/8/

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

1 Comment

Yes - @Cons7an7ine mentioned about jQuery in comment. Thank you too.

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.