0

I want to make a drop down menu containing list of all buildings while adding html to div id=teleport thru jquery.html(). while looping for populating the list, I don't know how to use a variable like BUILDINGS.name in option tag of select

1 Answer 1

0

I may be understanding you incorrectly, but it sounds like you want to generate a dropdown programatically using jQuery. Here is an example of how you might do this:

// declare some dummy data
var buildings = [];

buildings[0] = {
    'id': 'chrysler',
    'title': 'Chrysler Building'
    };

buildings[1] = {
    'id': 'empire-state',
    'title': 'Empire State Building'
    };

buildings[2] = {
    'id': 'white-house',
    'title': 'White House'
    };

// create the select tag element
var select = document.createElement('select');

// set the name (parameter name sent to server)
$(select).attr('name', 'building');

// loop through all the buildings
for (var i in buildings) {

    // create option tag element
    var option = document.createElement('option');

    // set 'value' for option tag (sent to server)
    $(option).attr('value', buildings[i].id);

    // set text for option tag (displayed to user)
    $(option).text(buildings[i].title);

    // add option tag to select tag
    select.addChild(option);
}

// add select tag to document
// TODO: update to insert at correct location in DOM
document.body.addChild(select);
  • Note: I have not tested this code, consider it to be pseudo-code with JavaScript-like syntax.

Sources:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .