0

I need to write the jQuery ready function and then: Inside the jQuery ready function, use jQuery to get all of the <h2> elements on the a new Git branch titled “headings” based off the “v0.1” tag with the following: page and assign them to a variable named headings; Without creating any new variables, change the text of the last element of the headings variable to: “I’m firstname lastname, and I’m learning jQuery!” (replace “firstname lastname” with your first and last name).

Here is the jQuery I have, yet keep getting an error:

$(document).ready(function(){   
    var headings = $('h2');
    $('headings').h2(3) 
                 .text ("I'm Arianna Delin, and I'm learning jQuery!");           
}).triggerHandler('change');

Can someone tell me what I am doing wrong?

4
  • 1
    you are missing }); at the end Commented Nov 20, 2015 at 22:54
  • 1
    1) $('headings') is running a query against the DOM using the "headings" selector: it won't find anything. What you mean there is just headings (the variable). 2) jQuery has no h2 function: if you want to retrieve the n-th element have a look at the selectors :nth-child() and :eq() (from what you said I would say to also have a look at :last and :last-child). 3) what is the reason for the triggerHandler? Commented Nov 20, 2015 at 23:06
  • $(document) .ready (function(){ var headings = $('h2'); $('h2') .eq (3).text (“I’m Arianna Delin, and I’m learning jQuery!”); }); Commented Nov 20, 2015 at 23:11
  • This still gives me an error. Commented Nov 20, 2015 at 23:11

1 Answer 1

1

Several issues:

  • Missing closing of $(document).ready(function(){ ... });
  • $('heading') is a new DOM search for elements "heading" and not the variable - You should remove the quotes - $(heading) or better use heading.last()
  • h2(3) not sure what you are doing there but if you want to target the last element then use .last();
  • If you are trying to get the third element the correct syntax will be .eq(2);

So consider this:

$(document).ready(function(){   
   $("h2").last().text("your text");           
}).triggerHandler("change");

//Or:

$(function(){                       // Same as ready();
   $("h2").eq(2).text("your text"); // changes the third element
   $(document).trigger("change");       
});

A Quick demo: JSnippet Demo

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

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.