Reputation: 798
I am trying to make a simple click to edit function for a page. Most of it works apart from when you click and edit multiple events. It always wanted to send the previous events again with the new click. Here's the code:
$('.editable').click(function() {
var that = $(this);
if (that.find('input').length > 0) {
return;
}
var currentText = that.text();
var name = that.data('field');
var contact_id = that.closest('[data-id]').data('id');
console.log(contact_id);
console.log(that);
that.empty();
console.log(name);
var $input = $('<input>').val( $.trim(currentText))
.css({
'position': 'relative',
top: '0px',
//left: '0px',
width: that.width(),
height: that.height(),
opacity: 1,
padding: '15px',
'margin-bottom': '0'
});
$(this).append($input);
$(document).on('blur','.editable', function(){
that.text( $input.val());
console.log(contact_id);
console.log(name);
that.find('input').remove();
});
p.s would you know why the on blur makes you need to click the element twice before clicking off to close it?
<div class="large-12" id="reviewsNeeded--main" data-id="<?php echo $result['id']; ?>">
<div class="collapse">
<div class="reviewsNeededTable--name large-12 columns" style="margin-bottom: 10px;">
<div class="large-6 small-12 columns">
<div class="large-5 no-pad-left editable" id="name" data-field="name">
<?php echo $result['first_name'] . " " . $result['last_name']; ?>
</div>
<div class="large-5 no-pad-left editable" style="font-size: 0.80rem; margin-top: 5px; color: #6c22ca;"
data-field="company">
<?php echo get_term_by('id', $result['company'], 'publishers')->name; ?>
</div>
</div>
<div class="large-6 columns no-pad-left">
<div class="large-12">
<button class="delete-btn" data-id="<?php echo $result['id'] ?>">
Delete
</button>
</div>
<div style="clear:both; margin: 5px 0"></div>
<div class="large-12">
<a class="show--meta">
<button>Show More</button>
</a>
</div>
</div>
</div>
<div class="reviewsNeededTable--request large-3 columns">
</div>
</div>
</div>
Upvotes: 0
Views: 26
Reputation: 64657
Every time you click you are adding a new blur handler on any .editable
, with that
scoped to the current context. It sounds like you want to change:
$(document).on('blur','.editable', function(){
to
$(that).one('blur', function(){
so that it fires only once (.one
) and only on the current input.
Upvotes: 3