Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

Parsing a value of a variable onto another variable in javascript

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
}
question from:https://stackoverflow.com/questions/65916693/parsing-a-value-of-a-variable-onto-another-variable-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}"]`);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...