0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2077648
  • 951
  • 7
  • 27
  • 42
  • @Balus this is not a duplicate , kindly refer the edited question. I want to access a normal liat in the java script without using JSON – user2077648 Apr 22 '16 at 03:37
  • If you're not interested in server side solutions, please remove server side aspects from the question. Rightclick page in browser, *View Source* and use that as kickoff snippet for the question. – BalusC Apr 22 '16 at 07:03

0 Answers0