Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

grab text from html and use it and concatenation to access object property

I don’t know if this is possible, and I’m pretty new to JavaScript and programing, but here is what I’m trying to do:

I have a table in HTML:

<table>
    <tr>
      <td id="w1t1">kc</td>
      <td id="w1s1">30</td>
    </tr>
</table>

I have an object in JS:
const kc = {name: kc, color: "red", score: 0};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would like to grab the text like so:
let team1 = document.getElementById("w1t1").textContent;

this should set the value of team1 to "kc".
I would then like to concatenate with ".score" to access kc.score like so:

function test(){
    let t1 = team1 + ".score";
    console.log(t1);
}

I would like the value of t1 to be 0, which is the value of kc.score, but instead it’s value is "kc.score".

is there anyway to get this to work?

>Solution :

As far I understand you want the score by team1 so here is how you can do this

// First store name of the team in a variable like you are doing
const team1 = document.getElementById("w1t1").textContent;
// now in team1 var we have value of kc

// modify your object a bit
const  team = { kc: {name: kc, color: "red", score: 0} };

console.log(team[team1].score)

Let me know if this works!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading