0
self.$el[0].options[0]

I get:

<option value="asd">asd</option>

self.$el[0].options[0].attr("selected","selected")

I get:

is not a function.

How do I modify the asd in js? Is it because it's an html element?

2
  • 1
    Please show the complete select. For example If $el[0] is a select, $el[0].value="asd"; will select the option - also $el[0].selectedIndex=0 will select the first option Commented Nov 12, 2018 at 8:56
  • 1
    are you using jQuery or some other framework like Vue? If you're using pure js the function should be .getAttribute instead of .attr Commented Nov 12, 2018 at 8:56

2 Answers 2

2

Your selector gets the DOM node. So, any jquery method won't get applied on DOM element. Instead you make it a jquery object:

$(self.$el[0].options[0]).attr("selected","selected")

or you can use setAttribute() of DOM method:

self.$el[0].options[0].setAttribute("selected","selected")
Sign up to request clarification or add additional context in comments.

Comments

0

you can use js API:

document.getElementByTagNames['option'][0].setAtttribute('selected', 'selected')

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.