Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm trying to write a form validation and I'm stuck with writing a selector for this select-option element without any other attribute.
<select multiple="multiple" name="formCountries[]">
How can I do this?
–
Try like this. See Attribute Selector
$('select[multiple = multiple][name="formCountries[]"]').change(function() {
You can use attribute selector to select by name. You have to escape the square brackets which could be done by wrapping the name in quotes.
Live Demo
var sel = $('[name="formCountries[]"]');
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#btn').click(function(){
alert('ding '+$('select').val());
</script>
</head>
<input type="button" id="btn"/>
<select multiple="multiple" name="formCountries[]">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.