user7172
user7172

Reputation: 974

How to get value from combobox in leaflet

From https://github.com/Leaflet/Leaflet/issues/936 I do : http://jsfiddle.net/nFLc3/3/

var legend = L.control({position: 'topright'});
legend.onAdd = function (map) {
    var div = L.DomUtil.create('div', 'info legend');
    div.innerHTML = '<select><option>1</option><option>2</option><option>3</option></select>';
    div.firstChild.onmousedown = div.firstChild.ondblclick = L.DomEvent.stopPropagation;
    return div;
};
legend.addTo(map);

$('select').change(function(){
    alert('changed');
});

How to get value selected value from combobox. I try something like:

$('select').change(function(){
    var x=document.getElementById("select");
    alert(x);
});

but it`s return null.

I think that this is a trivial problem, but I have a problem ..

Upvotes: 1

Views: 668

Answers (1)

Kishore Barik
Kishore Barik

Reputation: 770

in the first code snippet just use

$('select').change(function(){
    alert(this.value);
});

If it's too difficult for you to maintain this variable and you try something like in 2nd code snippet (getting value directly from element)....

As per assumption 1. your have tried to use getElementById for that you have to give your select an id

div.innerHTML = '<select id="mySelect"><option>1</option><option>2</option><option>3</option></select>';

and while retrieving

$('select').change(function(){
    var x=document.getElementById("mySelect").value;
    alert(x);
});
  1. you tried to use select you might be trying to get value by tag name. for that you have to

    $('select').change(function(){
            var x=document.getElementsByTagName("select")[0].value;
            alert(x);
        }); 
    

Note: getElementsByTagName returns an array of result, hence you have to keep the index proper if there are more than one select in the page and is difficult for maintainance. So try other options.

Upvotes: 2

Related Questions