1

I am trying to extract data from the option value. For example only ID or UserName. Can you please help?

$(document).ready(function() {
  $.getJSON("http://localhost:8080/EBCargoAndCommodityServices/resources/user-form/retrieve", function(data) {
    data.forEach(function(dt) {
      $("#customer-list").append('<option value="' + dt.id + dt.contactName + '">' + dt.contactName + '</option>');
    });
    $('#customer-list').change(function() {
      var str = this.value;
      alert(str);
    });
  });
});
2
  • 1
    What is being alerted in alert(str)? Commented May 22, 2020 at 15:52
  • It is retrieving id and name,however I will want to take control and extract the id and name seperately. Commented May 22, 2020 at 17:55

2 Answers 2

1
Let's say for example that dt.id = 1 and dt.contactName is Bartosz.
So you would be alerting 1Bartosz in alert(str)

While building up the options do this,

$("#customer-list").append('<option value="' + `${dt.id}_${dt.contactName}` + '">' + dt.contactName + '</option>'); // Using template literals and concatenating both id and contact name using a delimiter `_`

Then, in your change function do this.

const info = str.split('_')
/*
info will be an array  with two elements 
info[0] will be id  i.e. 1
info[1] will be contactName i.e. Bartosz
*/ 
Sign up to request clarification or add additional context in comments.

Comments

1

Sure you can, the best way would be to store the values you need to use into specific data attributes, then access them using the .data() method

    const $ = jQuery;
    $(document).ready(function() {
        // Mocked data for test
        const data = [{"address":"","companyName":"","contactName":"ghkkkkk","email":"","id":22,"surname":"","telephone":0},
        {"address":"","companyName":"","contactName":"asdasd","email":"","id":23,"surname":"","telephone":0}]
        data.forEach(function(dt) {
          $("#customer-list").append('<option data-id="' + dt.id + '" data-name="' + dt.contactName + '" value="' + dt.id + dt.contactName + '">' + dt.contactName + '</option>');
        });
        $('#customer-list').change(function() {
          const selected = $(this).find('option:selected');
          $('#result').text('selected id is: ' + selected.data('id') + ' contact name is : ' + selected.data('name'))
          
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <select id="customer-list">
    <option>select an option</option>
  </select>
  <span id="result"></span>

2 Comments

Thank you for you answer. I am getting undefined. My JSON Dummy Array looks as follows:[{"address":"","companyName":"","contactName":"ghkkkkk","email":"","id":22,"surname":"","telephone":0}]. I believe I need to access "nested" object first
I've changed the snippet to make test runnable :)

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.