JavaScript DOM Tutorial #4 - The Query Selector

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 101

  • @webapplicationguide3798
    @webapplicationguide3798 7 лет назад +46

    document.querySelector("#element") will return single element
    document.querySelectorAll(".name") will return collection of elements
    Thank You !!

    • @sparshgupta8078
      @sparshgupta8078 4 года назад +1

      but there's a difference between A HTML collection and a NodeList

    • @Human_Evolution-
      @Human_Evolution- 3 года назад +4

      NetNinja either made a mistake or JS changed some of its rules. Because as of now, you can run a .forEach(); on a document.querySelectorAll(); without having to turn it into an array with Array.from();.

    • @simple8810
      @simple8810 3 года назад

      @@Human_Evolution- Excatly you are right now we donot need to change that to an array when using querySelectorAll cux it gives us nodelist,
      but using classname instead of querySelectorAll,it need to change into array cause here we get htmlCollection
      Nd he has expalined this point in next video

  • @ChrisMits1
    @ChrisMits1 2 года назад +11

    querySelectorAll returns a NodeList in which we can apply a forEach() method directly!
    No need to convert it into an array!
    Your student from Udemy :)

    • @yacinemotivation3226
      @yacinemotivation3226 Год назад

      But he convert it into an array so I didn't see a difference between querySelector and getElementsBy ..

  • @amansharma-lp3fm
    @amansharma-lp3fm 6 лет назад +11

    Your teaching style is great everything is very clear i under stood css animations from your videos and now javascript thumps up love yr tutorials better then paid ones out there which i have but still watching yours
    ☺️☺️☺️☺️😊😊

    • @NetNinja
      @NetNinja  6 лет назад +1

      Thanks so much :D

  • @cherry-lp8yq
    @cherry-lp8yq 4 года назад +1

    after watching your first 4 videos of javascript, I started loving it after I decided to quit learning. you are the best :)))))

  • @DigitalEmma1
    @DigitalEmma1 3 года назад +1

    3 years later and this tutorial is still a Gold...

  • @zeroxcrusher
    @zeroxcrusher 5 лет назад +9

    So how is that different from getElementByID method? Don't they do the same thing?

  • @Cho1279624
    @Cho1279624 7 лет назад +2

    It's great for you to connect 'querySelector' to jQuery selector. I got it now. Thanks a lot!

  • @chrisdascoli6815
    @chrisdascoli6815 6 лет назад +4

    If you want to be REALLY fancy you can also convert the nodeList (which is what querySelectorAll returns) to an array by doing const books = [...document.querySelectorAll('#book-list li .name')]

    • @yizu7283
      @yizu7283 3 года назад +1

      Thank you for this comment! It helped me solve an issue in my code :-)

  • @christinec1674
    @christinec1674 2 года назад +1

    "this class has a span of name"...that made me smile and chuckle a little. so cute. 4:15

  • @genjimccorkle5518
    @genjimccorkle5518 3 года назад

    This works so nicely, especially when you can't find or dont have immediate access where the original code in linux and need a quick fix like changing an href attribute.

  • @jazzorgangrooves
    @jazzorgangrooves 7 лет назад +15

    4:16 - This class has a span of name. 😜 Great tutorial btw.

  • @sergeyphilippenko6533
    @sergeyphilippenko6533 7 лет назад +8

    I think, it is very impotrant to add differences between querySelectorAll and other methods. querySelectorAll return a static collection, while getElementsByClassNmae return dynamic one. If some element was dunamiclly added to a page, variable, which contain collection, would dynamicly changes

  • @frenchfry06
    @frenchfry06 7 лет назад +18

    Love your tutorials. Keep it up

  • @DJDELLANNIE
    @DJDELLANNIE 3 года назад

    I know this is a long time ago but thank you for the help. I finally understand how to use querySelectors now

  • @MrPretoriaS
    @MrPretoriaS 4 года назад +4

    At least in modern browsers .querySelectorAll() returns a NodeList, which has the .forEach() method

  • @mrcoder7327
    @mrcoder7327 5 лет назад +5

    I like vanilla javascript because I always wanted a challenge

  • @corbettknoff5123
    @corbettknoff5123 3 года назад

    This video is doubly amazing because of the nod to Patrick Rothfuss!

  • @shumonuddin6806
    @shumonuddin6806 5 лет назад

    I don't know why this video has fewer views, this could help tons of people who want to know more about JavaScript.

  • @LJ-lw8qd
    @LJ-lw8qd 2 года назад

    Just going through this set of learning in 2022 (One of the best teachers around) and found that .name (well for me anyways) should be span.name in order to get the same result.

  • @vladio
    @vladio 7 лет назад +14

    Ooohhh I get it, so that's why they called it "jQuery" 😃

  • @seekingcode
    @seekingcode 5 лет назад +1

    Thanks so much. That forEach trick is so useful!

  • @kalamashaka8280
    @kalamashaka8280 6 лет назад +2

    My consloe is displaying null and i have no errors:
    const wmf = document.querySelector('#book-list li:nth-child(2).name');
    console.log(wmf);
    var books = document.querySelector('#book-list li.name');
    console.log(books);

  • @cherychumchum7999
    @cherychumchum7999 4 года назад

    you made understanding so simple. Thank you very much!

  • @reginaldgideon6223
    @reginaldgideon6223 3 года назад

    cool tutorial it's easy to understand :)

  • @atleast_me
    @atleast_me 5 лет назад

    Thank you so much. You provide very useful information and you know how to teach.

  • @aadilmangera6902
    @aadilmangera6902 4 года назад +1

    An easier way of doing this -
    const wmf = document.querySelectorAll('#book-list .name')[1];
    wmf.style.color='red'

  • @dapolcio3405
    @dapolcio3405 5 лет назад +1

    You don't have to convert books to array to use forEach method when you use querySelectorAll

  • @justinafsari4197
    @justinafsari4197 6 лет назад +1

    You don't need to turn the books node list into an array, If you check the __proto__ it has a built in forEach method. Thanks for everything else though!!

  • @krishnakantpandey8886
    @krishnakantpandey8886 6 лет назад

    Thanks for making such video and I appreciate the time and efforts you've taken to share your knowledge with us..!!

  • @lexydestiny5849
    @lexydestiny5849 7 лет назад

    Dope Video.. About to say Goodbye to jquery before ending of this playlist

  • @rubhan94
    @rubhan94 6 лет назад +2

    I sort of get it all, except for where you get the "book" in ...forEach(function(book)... part. As I see it is colour coded the same way as document I assume it is something along the line of document. Sorry just starting to learn JS, if anyone could answer me I'd appreciate it! :D

  • @klausklemper5987
    @klausklemper5987 5 лет назад +3

    Hello.. At first ..Thanks for your Video .. But if i use
    const wmf= document.querySelector('#book-list li:nth-child(2).name');
    console.log(wmf); , as u did ... i get NULL. ( im using Chrome).

    • @klausklemper5987
      @klausklemper5987 5 лет назад +7

      ok i see my mistake now.. i have to make a space between ...child(2) .name'); tricky...

    • @jasnanajeem9121
      @jasnanajeem9121 4 года назад

      @@klausklemper5987 thanks a lot for pointing out the 'space' thing!!

    • @stevezct
      @stevezct 2 года назад

      @@klausklemper5987 thanks man

  • @sfitzsimmons0211
    @sfitzsimmons0211 5 лет назад

    Is this outdated? It seems .querySelector() only grabs HTML tags rather than classes/ids.
    If you want classes/Ids we use .getElementById()
    Or am I doing it wrong?

  • @maryamghorbani1333
    @maryamghorbani1333 3 года назад

    Super Duper

  • @donfeto7636
    @donfeto7636 5 лет назад

    Love your tutorials

  • @nabeeltech7885
    @nabeeltech7885 6 лет назад +1

    for me the query is not selecting and shows null on console.log

  • @pallabiduttabose3467
    @pallabiduttabose3467 2 года назад

    const wmf = document.querySelector('#book-list li:nth-child(2).name')
    console.log(wmf)
    It's showing null at the console. Can anyone help me with this?

  • @raniadesign57
    @raniadesign57 6 лет назад

    As usual, amazing tut

  • @mohamedyansane6595
    @mohamedyansane6595 2 года назад

    The querySelector() method returns the first element that matches a CSS selector.

  • @alexroman3120
    @alexroman3120 5 лет назад

    Hi! I have a problem to use querySelector with pseudo class before. Is there some way to choose element with it?

  • @akaarakoo
    @akaarakoo 2 года назад

    ninja why if i use keyword const it give me error and show Uncaught SyntaxError: Identifier 'wrap' has already been declared, but with keyword var its work ?

    • @MaxProgramming
      @MaxProgramming Год назад

      Maybe you used it in the browser console

  • @mosesabbey945
    @mosesabbey945 2 года назад

    i actually enjoy the video on this.. pls send the link with the form you design using htlm and css pls... i just check you github repo now but i cant find it... it will help me while i practice thank you very much

  • @dzmitryaksionau4742
    @dzmitryaksionau4742 4 года назад

    thanks bro, good lessons, nodeList has own forEach

  • @mariansand9663
    @mariansand9663 7 лет назад

    I highly appreciate your tutorials; they are concise, to the point and super helpful. Thanks a lot, subscribed!
    I’ve already worked through some of your JavaScript tutorials, especially through your complete ES6 tutorial. Therefore, I have a kind request: Could you please stick from now on to ES6? For someone like me to whom JavaScript/ES6 is still fairly new, it would simplify things, as I don’t have to double check whether there is an ES6 way of writing that code. Thanks!

  • @kagisokoos1766
    @kagisokoos1766 6 лет назад

    is it bad practice to use for(x = 0; x < books.length;x++){
    console.log(books[x]);
    }; beside using Array.from

  • @patrusiaklika6037
    @patrusiaklika6037 4 года назад

    Hello.I download file on your github is empty. Anyone can help?

  • @xMostFuriousx
    @xMostFuriousx 7 лет назад

    Why querySelector(#book-list li.name) returned the FIRST element and not the last ? Im kind of use to in other languages that the last element that is equal to your statment is returned. JS stops at first element ?

  • @ahmedfarid8691
    @ahmedfarid8691 4 года назад

    thank you for your great effort for helping us. I wanna ask you ask you a question. which is better, watching the all video then applying, watching a rule then applying then returning back to the video?

  • @FrankIsom
    @FrankIsom 5 лет назад

    How would I select certain elements.. example: I want to select only the first and third or second and last element

  • @0the0ambient0
    @0the0ambient0 4 года назад

    Great tutorial, as always.

  • @mandienk2702
    @mandienk2702 5 лет назад

    Pretty cool :-) thank you for this video

  • @kamaboko1
    @kamaboko1 7 лет назад

    Great tuts. So, given the greater control that querySelector offers, do you still find yourself using jQuery often? If so, is the primary reason for ease of cross browser compatibility? Maybe for Ajax?

  • @returnZeroo
    @returnZeroo 7 лет назад +5

    I hope, after finishing this course I can totally ignore jquery

    • @MADPURO
      @MADPURO 7 лет назад

      King Rayhan feels tempting to master jQuery but id rather master js itself first. Specifically the DOM.

  • @8o8inSquares
    @8o8inSquares 7 лет назад

    I love it

  • @syedusmanahmed
    @syedusmanahmed 7 лет назад

    Waiting for the next video.

  • @abett6003
    @abett6003 5 лет назад

    Thanks.

  • @MohamedRaffiM
    @MohamedRaffiM 2 года назад

    7:10 how to get the value inside the span tag

  • @debojyotichatterjee2845
    @debojyotichatterjee2845 6 лет назад

    const wmf = document.querySelector('#book-list li:nth-child(2).name');
    console.log(wmf);
    The above statement logs null value in the console...idk why

    • @yonk-oh7227
      @yonk-oh7227 5 лет назад +1

      Put a space after “(2)“
      The .name is a css class not a function/method. Hope you’ve become a great developer since I’m seeing this comment is from a year ago haha

    • @maciejpyc
      @maciejpyc 4 года назад

      @@yonk-oh7227 I hade the same null in console but I made another mistake- I've put tags abowe code not at the bottom of at index.html - maybe someone will be looking for the same Problem as mine :) (I had this mistake because Ninja did it in 1st video in this series: ruclips.net/video/FIORjGvT0kk/видео.html )

  • @arnulfoordiz4300
    @arnulfoordiz4300 6 лет назад

    I hope you make php or ruby tutorials

  • @shaq142
    @shaq142 7 лет назад +2

    books.forEach should work in all modern browsers.

    • @NetNinja
      @NetNinja  7 лет назад +1

      Thanks, you're correct, of course :). I've added a little edit to the next video (coming up soon) to explain.

    • @shaq142
      @shaq142 7 лет назад +1

      You can also do […document.querySelectorAll(‘.divy’)] with ES6 supporting browsers which ... are the same browsers that support nodelist.forEach. Anyways I disagree with writing ugly code to support old browsers. In my opinion just use polyfill.io/ and forget about old browsers. All functions neccessary will be pollyfilled. I think this is the best practice than just saying f*ck it, I'll just use jquery.

    • @shaq142
      @shaq142 7 лет назад

      Damn. The polyfill is tiny
      if (window.NodeList && !NodeList.prototype.forEach) {
      NodeList.prototype.forEach = function (callback, thisArg) {
      thisArg = thisArg || window;
      for (var i = 0; i < this.length; i++) {
      callback.call(thisArg, this[i], i, this);
      }
      };
      }

  • @takiahmed5575
    @takiahmed5575 2 месяца назад

    4:15 "This class has a span of name" 😭

  • @jdevcast6527
    @jdevcast6527 7 лет назад

    Really like your tuts! A quick question about the developer tools in Chrome. When I query the DOM I get back a ton of info in the console under 'span.name'. I just want the basic tags displayed like you showed in your video. Does anyone know how to do that?

  • @meganmatthews6809
    @meganmatthews6809 Год назад

    Everything I'm doing not working

  • @tiannimyers1204
    @tiannimyers1204 6 лет назад

    querySelector vs getElementById ?

  • @boboboboboboo7522
    @boboboboboboo7522 6 лет назад

    Awesome channel thank you for what you do. Just found this channel through a post by Brad Traversy. Do you have any udemy courses?

  • @davideugene7911
    @davideugene7911 7 лет назад

    Merci :)

  • @cheeseburger1884
    @cheeseburger1884 6 лет назад

    What is the theme being used in Atom?

    • @NetNinja
      @NetNinja  6 лет назад

      Hey, it's called Atom Monokai :)

  • @sparshgupta8078
    @sparshgupta8078 4 года назад

    remember everyone that there is a difference between HTML collection and a NodeList. so don't be confused when he is saying "a collection".

  • @ivonajankovic5928
    @ivonajankovic5928 6 лет назад

    First like than watching.

  • @hikolanikola8775
    @hikolanikola8775 6 лет назад

    "This class has a span of name" xD

  • @rishikeshct4502
    @rishikeshct4502 7 лет назад

    Hi dude,
    I want to suggest something from this u can earn money but it needs need some effort.
    Why don't you create udemy course on web development by creating one awesome project.
    There are lot of courses which teaches the htnl css js but many don't like those course because they wanted to learn a real time project.
    So if time permits go for it.

  • @cristianouzumaki2455
    @cristianouzumaki2455 4 года назад

    not everyone has worked on jquery .

  • @joejoe439
    @joejoe439 6 лет назад

    moving too fast can't of hard to see what your grabbing i know i should pause the video

  • @jakubwozniak3089
    @jakubwozniak3089 6 лет назад

    This is infuriating. Everywhere I go the tutorials about the querySelectors are the same.
    How do I use querySelector to loop through all my array objects and return the specific one?
    You have a list of books, can you turn it into an array and with an onclick method return that specific one?
    It's easy when you use nth-child(2) but when you have 11 elements this gets tedious.

    • @KillerKyuubi
      @KillerKyuubi 5 лет назад

      You've probably solved this already but for anyone else who needs to know, you can loop through all the elements with a for loop and grab each iteration and add a click event to it.