Domanda

<select>     
<div class="options">
              <option value="nothing">Nothing</option>
              <option value="snothing">Second Nothing</option>
      </div>
</select>

I want to get the content of the DIV and print it inside javascript in a selector.

$(document).ready(function () {
    var options = $('.options').html();
.
.
.
        var content = '<select>' + options + '</select>';
.
.
.

    });

I need to take options from the HTML. I've created a div indise <select></select> tags, because I had no other idea. But it's not working! I tried to put the options var inside <p></p> tags so i can print it, it sais 'undefined'.

È stato utile?

Soluzione

Assuming the missing > is a typo, your structure is invalid. select elements cannot have div elements as direct children.

You could do this:

<select class="options">
    <option value="nothing">Nothing</option>
    <option value="snothing">Second Nothing</option>
</select>

And then

var options = $('.options').html();

Live Example | Live Source

Altri suggerimenti

Firstly, remove the <div>. It's not helping you at all. Then, if you want just the options:

$("select").html(); // to get the HTML

or

var something = $("select options"); // to get an array of jQuery objects

What you want, I think, is: $('select').html(options) or $('select').append(options) where options is an html string, e.g. "<option value='1'>One</option>", or a jquery element collection e.g. $('option').

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top