Question

I'm trying to make a function that adds a row but my function on keypress didn't worked/. I've trying with ", ', and without which makes no difference. When I press on the button nothing happened.

//function append lundi
var counterlundi= 0;
var $newRow ; 
$(function(){
    $('#add_lundi').click(function(){
        counterlundi += 1;
        $('#numlundi').append($newRow = $('<input id="numlundi' + counterlundi + '" name="numlundi[]' + '" type="text" onblur="autre();" onfocus="enter();"/> '
    )
)

how Do I do?

Here my php of my static input of autocomplete

<td>
    <b>Lundi</b> </br><?php echo $date2 ?>
    <div id="lundicon"> 
        <p id="add_lundi" class="plus"><a href="#"><span> + </span> </a></p>
    </div>
</td>
<td>
    <div id="numlundi">
        <input  onblur="autre();" onfocus="enter();" size="10" type="text" id="projlundi" name="projlundi"onkeypress="return handleEnter2(event, this, 'task');"/>
    </div>
</td>
<td>
    <div id="clientlun"> 
        <input type="text" name="clientlundi" onblur="autre();" onfocus="enter();"  class = "client" size="12" id ="clientlundi" readonly />
    </div>
</td>
<td>
    <div id="prodesclun">
        <input type="text" name="projdesclundi" onblur="autre();" onfocus="enter();" size="30" class "desc" id ="projdesclundi"readonly />
    </div>
</td>

My Javascript function of my Static input

//-------AUTO COMPLETE LUNDI PROJET-----
                   $("#projlundi").autocomplete({
                    source:'getautocomplete.php',
                    minLength:1
                });
function handleEnter2(e, obj, field){
    if (e.keyCode == 13 || e.which == 13){
        if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    tempArrayInJS = JSON.parse(xmlhttp.responseText); 
                    $("#clientlundi").val( tempArrayInJS[0]['cliName']);    
                    $("#projdesclundi").val( tempArrayInJS[0]['proDescription']);
                }
            }
            xmlhttp.open("GET","completeclient.php?q="+obj.value,true);
            xmlhttp.send();
    }
}
Was it helpful?

Solution

$('#numlundi').on('keypress', 'input', function(e){
    var $this = $(this);
    if (e.keyCode == 13 || e.which == 13){
    if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                tempArrayInJS = JSON.parse(xmlhttp.responseText); 
                if ($this.attr('id') != 'numlundi') {
                    var id = $this.attr('id').replace('numlundi', '');
                    $("#clientlundi"+id).val( tempArrayInJS[0]['cliName']);    
                    $("#projdesclundi"+id).val( tempArrayInJS[0]['proDescription']);
                } else {
                    $("#clientlundi").val( tempArrayInJS[0]['cliName']);    
                    $("#projdesclundi").val( tempArrayInJS[0]['proDescription']);
                }
            }
        }
        xmlhttp.open("GET","completeclient.php?q="+$this.val(),true);
        xmlhttp.send();
}
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top