I have a list in HTML which I need to access in the java script to perform certain validation.
Below is the list skills that populates the drop down :
HTML
<select class="skill-select form-control" name="name" required="true" id="skill-name">
<option></option> <!-- Needed for select2 -->
<c:forEach items="${skills}" var="skill">
<option value="${skill.id}">${skill.name}</option>
</c:forEach>
</select>
I have tried using the below Javascript but nothing seems to happen:
$(".skill-select").change(function(){
var list = ${skills};
$.each(list, function( index, value ) {
alert( index + ": " + value );
});
});
Here, I am using Spring MVC to set the object skills is a list and hence it is not in json format to access it easily in the java script.
Could you please let me know how to access the skills list in the java script without converting it into json format?
Currently I am using the below java script :
$(".skill-select").change(function(){
var opts = $('#skill-name')[0].options;
var array = $.map(opts, function(elem) {
return (elem.text);
});
for (i = 1; i < array.length-1; i++) {
if ($('#skill-name :selected').text() == array[i]) {
$('#failure-msg').html('Could not add existing skill name. Please enter a new skill name and try again.');
$('#failure-msg').removeClass('hidden');
}
}
});
Is there any other efficient way of achieving the same by accessing the list in the javascript?