I am appending a drop down menu on the text input, i have the gender as male, and female i am just adding this in my li’s while it get foucs. but i am getting additionally 2 empty li’s on focus.. why?
this is my code :
$(document).ready(function(){
var gender = ['Female','Male'],
genderField = $('#gender');
var genderUpdate= function(){
genderField.focus(function(){
if(!$('#genderList').length){
var topPos = $(this).position().top,lftPos = $(this).position().left;
$('<ul id="genderList"></ul>').css({
position :'absolute',
top : topPos,
left : lftPos
}).appendTo($(this).parent());
$.map(gender,function(val,i){
$('<li>'+val+'<li>').appendTo('#genderList');
console.log('append');
})
}
})
}
genderUpdate();
})
here is the fiddle :
what is wrong here?
Find the difference ;=)
The browser has been “nice” and automatically closed your unclosed li tags, two on each iteration of the loop.