1

I gave some options some values in html and I want to save these values in a js var and I wrote the following line but it didn't work

for (let node of document.getElementsByClassName('op.e.target.value')) {
values.push(node.value);
}

(where op is the class given to all the values)

6
  • if op is the class name what is .e.target.value? Is that other classes the elements also need to have to be selected? Commented Jun 19, 2020 at 14:59
  • var x = document.getElementsByClassName("Class Name"); Commented Jun 19, 2020 at 15:00
  • .e.target is like a function that gets you all the information about the selected element which is the class op and adding .value gets you specifically the value of it Commented Jun 19, 2020 at 15:01
  • if I passed the class name it will return the text of the option and I want the value attribute @DomadiyaBhautik Commented Jun 19, 2020 at 15:02
  • 1
    The only time I've seen e.target is in relation to events, e.g. on click to get the clicked element. Anyway this wouldn't be where you'd use it Commented Jun 19, 2020 at 15:12

1 Answer 1

2

Firstly, getElementsByClassName accepts class names as its argument only, not a function name in a string

When you're using getElementsByClassName you're storing an array of html elements, so to get the values of each you would need to for loop through it.

var values1 = document.getElementsByClassName('op');

var values = [];
for (let node of values1) {
    values.push(node.value);
}

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.