Home > Net >  How do I run querySelector backwards?
How do I run querySelector backwards?

Time:01-27

In a document, there are 2 elements with the class name "div._13NKt.copyable-text.selectable-text". (It's web.whatsapp.com)

One on top of the document, one on the bottom of the document.

I would like to querySelect the 2nd element / the element on the bottom.

How could I do this?

Is there an option to do a querySelect backwards?

Here is my code so far:

document.querySelector("div._13NKt.copyable-text.selectable-text").focus();

I am running this command using mobileFX web brower based on Chromium.

I call it like this:

webkit1.Eval("document.querySelector(" & Quote("div._13NKt.copyable-text.selectable-text") & ").focus();")

CodePudding user response:

Something like this?

const elements = document.querySelectorAll("div._13NKt.copyable-text.selectable-text");
if(elements.length) elements[elements.length - 1].focus();

CodePudding user response:

I think you can use document.querySelectorAll("div._13NKt.copyable-text.selectable-text") and then access to the index.

//Capture the elements
let myVar = document.querySelectorAll("div._13NKt.copyable-text.selectable-text");

//Then access to the index, taking into account that the initial index is 0.
console.log(myVar[1])
//Or
var secObject = myVar[1];
  •  Tags:  
  • Related