Coder Social home page Coder Social logo

js_selectors's Introduction

Selecting Elements with Javascript

We already know about selecting elements of our HTML with CSS selectors, so that we can give them the styling of our choice. You can also select elements with javascript functions, which will allow us to do more than just change their styling.

Our js selecting functions

Here are the big 5 functions you should know for selecting elements:

  • document.querySelector: This function takes as an argument a query string, which you're already used to from working with CSS selectors. It always returns the first element on the page that meets its criteria. So for example:
    • document.querySelector('h2') would give you the first <h2> element that appears on the page
    • document.querySelector('.fancy') would give you the first element on the page that has a class of fancy
    • document.querySelector('#hero-image') would give you the first element on the page that has an id of hero-image
  • document.querySelectorAll: This is just like document.querySelector, except it finds all the elements on the page that meet its criteria. It will return them in an array.
  • document.getElementsByTagName: This will give you all the elements that have the tag name you specify, for example document.getElementsByTagName('h2')
  • document.querySelector.getElementsByClassName: This will give you all the elements that have the class you specify, for example document.getElementsByClassName('fancy'). Note that there is no . at the start of that string!
  • document.querySelector.getElementById: This will give you just one element that has the id you specify, for example document.getElementById('hero-image'). Note that there is no # at the start of that string! Why do you think this function returns 1 element while the above 2 return arrays of elements?

Getting some practice

In main.js, there are some prompts for you to follow. Don't forget to link main.js to your html file!

js_selectors's People

Contributors

gndm avatar pmacaluso3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.