What's new

Help Question for bot

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,927
Reaction
472
Points
658
Age
24
create a jquery code that will display the data from array to dropdown.
For example

the array will be
'data' => [
'One'=>'1',
'Two'=>'2',
],

in the dropdown options looks like

<option value="1">One</option>
<option value="2">Two</option>
 
Sure! Here's an example of jQuery code that will dynamically display the data from the given array into a dropdown:

HTML:
HTML:
<select id="myDropdown"></select>

jQuery:
JavaScript:
$(document).ready(function() {
  var data = {
    'One': '1',
    'Two': '2',
  };

  var dropdown = $('#myDropdown');

  $.each(data, function(key, value) {
    dropdown.append($('<option></option>').attr('value', value).text(key));
  });
});

Explanation:
1. The data variable contains the array you provided, where the keys represent the display values and the values represent the option values.
2. The dropdown variable selects the dropdown element using its ID.
3. The $.each() function iterates over each key-value pair in the data array.
4. For each pair, a new option element is created using the text() and attr() functions to set the display value and option value accordingly.
5. Finally, the append() function adds the newly created option element to the dropdown.

Make sure to include the jQuery library in your HTML file for this code to work properly.
 
Sure! Here's an example of jQuery code that will dynamically display the data from the given array into a dropdown:

HTML:
HTML:
<select id="myDropdown"></select>

jQuery:
JavaScript:
$(document).ready(function() {
  var data = {
    'One': '1',
    'Two': '2',
  };

  var dropdown = $('#myDropdown');

  $.each(data, function(key, value) {
    dropdown.append($('<option></option>').attr('value', value).text(key));
  });
});

Explanation:
1. The data variable contains the array you provided, where the keys represent the display values and the values represent the option values.
2. The dropdown variable selects the dropdown element using its ID.
3. The $.each() function iterates over each key-value pair in the data array.
4. For each pair, a new option element is created using the text() and attr() functions to set the display value and option value accordingly.
5. Finally, the append() function adds the newly created option element to the dropdown.

Make sure to include the jQuery library in your HTML file for this code to work properly.
Please revise the code, the data in in php is looks like
'data' => [
'One',
'Two',
],
How can i display this in dropdown
 
Please revise the code, the data in in php is looks like
'data' => [
'One',
'Two',
],
How can i display this in dropdown
Apologies for the confusion. If the data in PHP is an array of values without corresponding keys, you can modify the jQuery code as follows:

HTML:
HTML:
<select id="myDropdown"></select>

jQuery:
JavaScript:
$(document).ready(function() {
  var data = [
    'One',
    'Two',
  ];

  var dropdown = $('#myDropdown');

  $.each(data, function(index, value) {
    dropdown.append($('<option></option>').attr('value', value).text(value));
  });
});

Explanation:
1. The data variable contains the array of values from PHP.
2. The dropdown variable selects the dropdown element using its ID.
3. The $.each() function iterates over each value in the data array. Since there are no corresponding keys, we use the index parameter to track the current iteration.
4. For each value, a new option element is created using the text() and attr() functions. We set both the value and text of the option to the current value in the iteration.
5. Finally, the append() function adds the newly created option element to the dropdown.

Make sure to include the jQuery library in your HTML file for this code to work properly.
 

Similar threads

Back
Top