How To Iterate A Result Of Jquery Selector May 06, 2023 Post a Comment I want to iterate a result of query selector. Html code nav1 Solution 1: Use $.each $("#navigation > a").each(function() { console.log(this.href) }); Copy $('#navigation > a')[0] ^ ^---- Selects the 1st dom object from the jQuery object | that is nothing but the index of the element among | the list of elements |------- Gives you children of nav(3 anchor tags in this case) which is a jQuery object that contains the list of matched elements Copy Solution 2: If you want to iterate all <a> tags, you can use each function $('#navigation >a').each(function() { alert(this.href); }); Copy and if you only want to get the first <a> tag then use .eq() alert($('#navigation >a').eq(0).attr('href'); Copy Solution 3: Use first() like this: var element = $("#navigation>a").first(); console.log(element); Copy Reference Solution 4: In jQuery, when you use index like [0], it means you are access the DOM element. That is whyBaca JugaCkeditor - Stylesheet Parser - Valid SelectorsKineticjs Undo Layers: Layers Don't Disappear On Undo?Match Element To Part Of A Data Attribute Value? $("#navigation >a")[0] Copy returns <a> tag. In order to iterate a jQuery selector result, use each $("#navigation >a").each(function(index, elem){ }); Copy Solution 5: You can use jQuery built-in each() for this iteration like this: $("#navigation>a").each(function(index){ console.log("I am " + index + "th element."); //and you can access this element by $(this) }); Copy Share You may like these postsJavaScript/jQuery Code Block Is Not Updating Parse.com Class Object CorrectlyTrying To Show Chessboard Js In Odoo Form Widget, No Error No PiecesHow To Do Per-page Javascript With The Rails Asset PipelineHow I Can Select Element With Double Class With QuerySelector() Post a Comment for "How To Iterate A Result Of Jquery Selector"
Post a Comment for "How To Iterate A Result Of Jquery Selector"