0

How can I parse the value of the constant named currentTab to that of the one named tabToActivate?

function panel(event) {
    const tab = event.currentTarget.closest(".tab");
    const container = tab.closest(".login-sign");
    const currentTab = event.currentTarget.dataset.forTab;
    const tabToActivate = container.querySelector(".tabcontent[data-tab ='${currentTab}']");
    
    console.log(tabToActivate)//outputs null perhaps due to improper parsing
}
4
  • 2
    I guess, it would make sense to share corresponding html to make sure your query is valid. Commented Jan 27, 2021 at 9:57
  • "perhaps due to improper parsing" - You're not parsing anything in your excerpt. Commented Jan 27, 2021 at 10:01
  • ".tabcontent[data-tab ='${currentTab}']" - a template literal uses backticks and not regular quotes Commented Jan 27, 2021 at 10:01
  • You have to use template literals if you want to interpolate the variable value as you are trying to do: const tabToActivate = container.querySelector(`.tabcontent[data-tab ='${currentTab}']`); Commented Jan 27, 2021 at 10:02

1 Answer 1

2

Though your question is not too clear but i think i got it. We can only pass a variable in a string by using back ticks not quotes;

const tabToActivate = container.querySelector(`.tabcontent[data-tab="${currentTab}"]`);
Sign up to request clarification or add additional context in comments.

1 Comment

Please wrap the attribute value in quotes to get a valid selector: ="${currentTab}"

Your Answer

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.