What's the best way to get the underlying DOM element by ID in jQuery?

我只是一个虾纸丫 提交于 2019-12-01 14:31:39

There are jQuery ways to get the value of the <select> that don't require you to access the actual DOM element. In particular, you can simply do this to get the value of the currently selected option:

$('#mySelect').val();

Sometimes, however, you do want to access a particular DOM attribute for whatever reason.

While the .get(0) syntax you provided is correct, it is also possible without the function call:

$("#mySelect")[0].selectedIndex;

A jQuery collection behaves as an array-like object and exposes the actual DOM elements through it.

ScottE

$("#mySelect").val() will do the trick.

$("#mySelect option:selected").val()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!