I have a bootstrap checkbox, with dynamic elements inside:
<div class="btn-group" style="margin-bottom:5px; width: 100%;">
<a id="urlFieldNameButton" class="btn btn-default btn-block dropdown-toggle btn-select" style="width: 100%;" data-toggle="dropdown" href="#">Select URL field name<span class="caret"></span></a>
<ul id="urlFieldName" class="dropdown-menu scrollable-menu" style="width: 100%;">
</ul>
</div>
Here is an example: http://www.bootply.com/9CvIygzob8
The ajax function that loads the combobox elements looks like this:
function FillCombo(json, combo) {
for (var i = 0; i < json.length; i++) {
var li = document.createElement("li");
var a = document.createElement("a");
a.innerHTML=json[i];
a.setAttribute("href","#");
//a.setAttribute("onclick", "selectElement()");
a.onclick = function(){ selectElement(a); };
li.appendChild(a);
combo.appendChild(li);
}
}
And here is a function that triggers when I am pressing on dynamically uploaded anchor:
function selectElement(a) {
var selText = a.innerHTML;
$("#urlFieldNameButton").html(selText+' <span class="caret"></span>');
}
I need to pass <a> tag as the parameter, because "this" object is equal to window, not to <a>, as it would be with static version of this function call.
The problem is that when I press on any of dynamically uploaded <a> of this list, the function always receives the last one(even when I press on first). Is there a better way to work with dynamic content?