0

How to convert this jquery code

$(element).find('.ui.dropdown') 

to pure JS?

I need to convert a simple code in JQUERY to pure JS.

The element is a variable in my code, it comes from this code:

$.each($("[action]").toArray(), function(key, element)
fdomn-m
28.8k8 gold badges39 silver badges70 bronze badges
asked May 11, 2020 at 15:04
5
  • 1
    document.querySelectorAll("[action]").forEach(function(element){}); Commented May 11, 2020 at 15:07
  • 3
    youmightnotneedjquery.com Commented May 11, 2020 at 15:08
  • What is the value will element hold? Can you console.log(element) and update the question Commented May 11, 2020 at 15:26
  • 1
    @SivakumarTadisetti From the bit of code that confused everyone: element will be a DOM node because they use .toArray(). It's terrible code, but that's what's been provided. Commented May 11, 2020 at 15:29
  • what do you want to do with the array? probably array.map can be the best choice if you just want to find the elements and convert them to an array. Commented May 11, 2020 at 18:29

1 Answer 1

1

How about the following?

Array.prototype.slice.call(document.querySelectorAll('[action]')).forEach(function (element, index) {
 element.querySelector('.ui.dropdown')
 // do something to the element
});

As noted, Array.prototype.slice() is only needed if the browser you're running doesn't support NodeList.prototype.forEach(). For modern browsers you can actually do:

document.querySelectorAll('[action]').forEach(function (element, index) {
 element.querySelector('.ui.dropdown')
 // do something to the element
});
answered May 11, 2020 at 15:07
Sign up to request clarification or add additional context in comments.

22 Comments

yep, just for better browser support
Any browser that doesn't support .forEach() on a node list also won't support Array.from. For best support use. Array.prototype.slice.call(document.querySelectorAll()).forEach(function(){});
I think OP wants to convert $(element).find('.ui.dropdown') to Vanilla JS
@HereticMonkey in case you still haven't seen it, read the title of the first revision.
@HereticMonkey personally I don't like the whole "you don't need to use jquery" - no, we don't need to but 20+ lines of code for 1 jquery line... think I'll keep using it thanks. So I comment that link and also normally move on - but was interested in the silly argument about which feature is "more compatible" with "older" versions of browsers (when neither is...). Then Sivakumar made the edit that removed all the formatting and I got too involved for my own good :)
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.