3

I need to append extra data to my select box options. here is my code.

Html:

<select id="questionNumber" class="form-control">
   <option>Select Number</option>
</select>

JQuery:

$.each(result.data, function (key, value) {
    $("#questionNumber").append($('<option>', {
        value: value.id,
        text: value.number
    }));
});

So i can add value and text but i want to append attributes also to this appended option like

data-marks = value.mark

So my expected result would be like

<select id="questionNumber" class="form-control">
   <option>Select Number</option>
   <option value="170" data-mark="2">1</option>
   <option value="171" data-mark="3">2</option>
</select>

What should i do for this? Thanks.

1

4 Answers 4

3

Here you go with the solution https://jsfiddle.net/u5vek2jc/

var result = {data:[
	{
  	id:"5",
    number: "5"
  },
  {
  	id: "89",
    number: "89"
  }
]}
$.each(result.data, function (key, value) {
    $("#questionNumber").append($('<option>', {
        value: value.id,
        text: value.number,
        'data-mark': value.id
    }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="questionNumber" class="form-control">
   <option>Select Number</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

2

You can add the data attribute to the HTML you generate. Also note that you can amend the logic so that the DOM is only updated once, instead of inside each iteration. Try this:

var html = result.data.map(function(o) {
  return `<option value="${o.id}" data-mark="${o.mark}">${o.number}</option>`;
}).join('');
$("#questionNumber").append(html);

1 Comment

As I mentioned on your answer, using data() means that the attribute will not appear in the DOM. The OP stated that as a requirement. It's possible that data() may meet their needs too
1

iterate over your data to create options, and then append it to your select element

$html='';
$.each(result.data, function (key, value) {
    html +='<option value="' + value.id + '"  data-mark="'+value.mark+'">' + value.number + '</option>';
});
$("#questionNumber").append(html);

1 Comment

@31piy this is exactly what the OP wants, and is much faster than performing a DOM operation once per iteration
0

This is a similar example and more complete:

var cadena_user_pais = ($(this).attr('id')).split("_");
var id_user_pais = cadena_user_pais[2];
var request = 'view_tipoNIF';
//tipo de NIF
        $.ajax({
            url: "fetch_single_users.php",
            method: "POST",
            data:{id_user_pais:id_user_pais,request:request},
            dataType: "json",
            success:function(data)
            {
                //alert('hola');
                var html='';
                $.each(data, function (key, value) {
                    html +='<option value="' + value.id + '">' + value.NIF + '</option>';
                });             
                $("#select_user_tipoNIF").append(html);
            }   //success
        });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.