0

want to extract the value of value attribute in the code below by it's inner text using javascript.

<label for="played-123">
 <input id="played-123" name="players[]" type="checkbox" **value="123"**>
 <img class="team" src="teams/abc.png" alt="abc"> Abc Xyz</label>

Here is what I want to extract from above html 123.

Any help will be appreciated. Thank You.

Error
  • 37
  • 5
  • 1
    Have you googled or researched this? What part of your attempt is not working? Where is your attempt? – Kinglish Feb 24 '22 at 15:38
  • And `value` attribute of ???? – Nalin Ranjan Feb 24 '22 at 15:39
  • In the above html code, you can see **value=123** under input tag. want to extract this **123** using the inner text by js. actually don't know much about java just researching it where I need. Thanks – Error Feb 24 '22 at 15:54
  • 1
    Sorry, you need to make an attempt at solving your own problem first before asking here. Do a little research and show us what you've tried. We're not a free code writing service – j08691 Feb 24 '22 at 15:58
  • Welcome to SO! Please take the [tour] and read "[ask]", "[Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648)" and their linked pages. Your question shows no effort toward solving the problem, so it looks like you want us to recommend a tutorial or documentation, or write code for you, both of which are off-topic. Instead, research the problem, write code, then when you run into a problem you can't figure out, ask a question about that particular issue. Anything prior to doing that is off-topic. – the Tin Man Feb 25 '22 at 22:24

1 Answers1

1

I did it myself, using:

function get_elements_by_inner(word) {
    res = []
    elems = [...document.getElementsByTagName('label')];
    elems.forEach((elem) => { 
        if(elem.outerHTML.includes(word)) {
            res.push(elem)
        }
    })
    return(res)
}

from @Cybernetic in "How to get element by innerText":

get_elements_by_inner("Abc Xyz")[0].firstElementChild.attributes.value.nodeValue;

This returned my desired value.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Error
  • 37
  • 5
  • It's good that you solved the problem, however your question needs work and is being flagged for deletion as a result. See my comment above. – the Tin Man Feb 25 '22 at 22:25