javascript - Printing list of options from a select element in Developer Tools - Stack Overflow

I am trying to pull the list of values from a select element while in Developer Tools console for Chrom

I am trying to pull the list of values from a select element while in Developer Tools console for Chrome (or Firefox), but I'm having trouble with the various answers I've seen in other places are a few years old and don't seem to work for me.

Take the following as an example:

<select id="states">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="AR">Arkansas</option>
  <option value="CA">California</option>
</select>

My selector on the console would be:

$$('#states > option').values

Values returns an ArrayIterator. But I'm not sure what to do with that.

What I would like to have returned are the value of each option and the text of each option.

I am trying to pull the list of values from a select element while in Developer Tools console for Chrome (or Firefox), but I'm having trouble with the various answers I've seen in other places are a few years old and don't seem to work for me.

Take the following as an example:

<select id="states">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="AR">Arkansas</option>
  <option value="CA">California</option>
</select>

My selector on the console would be:

$$('#states > option').values

Values returns an ArrayIterator. But I'm not sure what to do with that.

What I would like to have returned are the value of each option and the text of each option.

Share Improve this question edited May 13, 2022 at 3:49 Machtyn asked Apr 18, 2016 at 17:01 MachtynMachtyn 3,2808 gold badges44 silver badges69 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Unsure of how you want your data, you could push them to an array, or an object? Here's an example of getting an array of arrays, each sub array containing [val,text]:

var states = [];
$('#states option').each(function(){
    states.push([$(this).val(),$(this).text() ]);
});
console.log(states);

EDIT: Try this, doesn't use jQuery, will just log the info out for you, but you could push it to an empty array like above if you wish.

var states = $$('#states > option');
for (var i = 0; i < states.length; i++) {
    console.log(states[i].value, states[i].innerHTML);
}

You can try this:

const statesEl = document.getElementById('states');
const options = statesEl.getElementsByTagName('option');

const optionValues = Array.prototype.map.call(options, function(optionEl) {
  return {
    value: optionEl.getAttribute('value'),
    text: optionEl.textContent
  };
});

Then you can use optionValues array

    const statesEl = document.getElementById('states');
    const options = statesEl.getElementsByTagName('option');

    const optionValues = Array.prototype.map.call(options, function(optionEl) {
      return {
        value: optionEl.getAttribute('value'),
        text: optionEl.textContent
      };
    });
    
    console.log(optionValues);
<select id="states">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="AR">Arkansas</option>
  <option value="CA">California</option>
</select>

You can get the values like this:

 var states = $('#states > option');
 states.each(function(){ 
    console.log(this.value);
    console.log(this.text);
 }); 

Thank you all for your assistance. Every answer got me close to my solution. For some reason "each" was not working for me, but "map" did.

Here's my answer:

var states = $$('#states option')
states.map(function(state) {
  console.log(state.value, state.text);
});

You can do this with just this:

$('#states > option').each(function (){ 
    console.log(this.value, this.text);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744445770a4574594.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信