Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  136] [ 1]  / answers: 1 / hits: 35145  / 12 Years ago, thu, february 7, 2013, 12:00:00

I have written my code such that when user double clicks on a <td> element I am:




  • appending am <input> of type=text

  • adding a value to it and update it if the user clicks on enter



Here is the my problem:


If user double clicks on <td> and clicks on another <td> without pressing enter, I need the initial <td>'s <input> to be reset to previous value.



// Selecting the table <th> odd elements
$(#div table td).dblclick(function(){
var currentEle = $(this);
var value = $(this).html();
updateVal(currentEle, value);
});

function updateVal(currentEle, value)
{
$(currentEle).html('<input class=thVal type=text value='+value+' />');
$(.thVal).focus();
$(.thVal).keyup(function(event){
if(event.keyCode == 13){
$(currentEle).html($(.thVal).val().trim());
}
});

$('body').not(.thVal).click(function(){
if(('.thVal').length != 0)
{
$(currentEle).html($(.thVal).val().trim());
}
});
}


Please help me.
I don't want to use jeditable datatable.


More From » jquery

 Answers
133

Here in your case you need .stopPropagation(): http://jsfiddle.net/jFycy/



$(function () {
$(#div table td).dblclick(function (e) {
e.stopPropagation(); //<-------stop the bubbling of the event here
var currentEle = $(this);
var value = $(this).html();
updateVal(currentEle, value);
});
});

function updateVal(currentEle, value) {
$(currentEle).html('<input class=thVal type=text value=' + value + ' />');
$(.thVal).focus();
$(.thVal).keyup(function (event) {
if (event.keyCode == 13) {
$(currentEle).html($(.thVal).val().trim());
}
});

$(document).click(function () { // you can use $('html')
$(currentEle).html($(.thVal).val().trim());
});
}


Instead doing click on body do the event on document or html which is the parent elem of all others elems.


[#80378] Tuesday, February 5, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carlton

Total Points: 373
Total Questions: 123
Total Answers: 97

Location: Saint Helena
Member since Wed, Nov 9, 2022
2 Years ago
carlton questions
Sun, Apr 25, 21, 00:00, 3 Years ago
Wed, Feb 17, 21, 00:00, 3 Years ago
Tue, Oct 27, 20, 00:00, 4 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Mon, Apr 13, 20, 00:00, 4 Years ago
;