Skip to content

Instantly share code, notes, and snippets.

@vrandall66
Last active August 26, 2019 17:08
Show Gist options
  • Save vrandall66/beb31ac24fd4446f8553a84097e1596a to your computer and use it in GitHub Desktop.
Save vrandall66/beb31ac24fd4446f8553a84097e1596a to your computer and use it in GitHub Desktop.

jQuery Getters and Setters

<h1 class="main__article--h1" id="main-header">

let $h1 = $('#main-header');

1. How do you get the text from an element (say a paragraph element)?

$h1.text();

2. How do you set the text from an element (say a paragraph element)?

$h1.text('Hello');

3. How do you get the text from an input element?

let $username = $('#article__form--username');

$username.val()

4. How do you set the text from an input element?

$username.val('username')

5. How do you add, remove, or toggle classes of HTML elements?

.toggleClass()

$h1.toggleClass( 'hidden' );

    OR
    
$h1.toggleClass( 'hidden', addOrRemove );

6. How can you tell if an HTML element contains a certain CSS class?

$h1.hasClass( 'header' );

    //returns false;
    
$h1.hasClass( 'main__article--h1' );

    //returns true;

7. What happens when you select and element using vanilla JavaScript (document.querySelector()) and then try to use a jQuery method for getting the text of that element? Provide examples of this and what errors you see.

8. If you have a jQuery selector that returns multiple elements, say $('.link-img'), then do you need to use a for loop to change the elements? Why or why not? What is returned by the selector?

9. Create a demo (in CodePen) getting the text and setting the text of different elements.

10. Where do you go to find the official jQuery docs?

https://api.jquery.com

11. On this doc page, there are two parts of the page, each with a black header: one text() and the other text( text ). What is the purpose of these two different sections? Can you find other examples where a page is written/organized in this way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment