1

I am trying to change some can which depends in jQuery to pure JS and I was wondering how I can translate this one:

$("#mydiv .car span").text(myVariable);

What would the code above be in pure JS?

2
  • 3
    document.querySelector("#mydiv .car span").textContent = myVariable; Commented Oct 8, 2017 at 21:39
  • 1
    Have you done any research how to target elements using selectors and how to change the property? The comment above will work but if you want to learn from it you can read the documentation here: .querySelector() and .textContent() Commented Oct 8, 2017 at 21:40

1 Answer 1

3

You can do it using document.querySelector() and textContent like this:

document.querySelector("#mydiv .car span").textContent = myVariable;

Or using innerText like this :

document.querySelector("#mydiv .car span").innerText = myVariable;

MDN Reference for document.querySelector()

Returns the first Element within the document that matches the specified selector, or group of selectors.

Which makes it similar to jQuery with the possibility of using this kind of selector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.