I've got a set of divs, that behave a bit like input boxes. When user clicks "edit my details" button, he should be able to edit these boxes. When editing the boxes, I want the current content of the box to be selected when user clicks anywhere on the box. Problem is that label of the box covers part of it and I don't know how to address the actual box, that is below the label to be selected.
Here's a link with simplified version of the problematic code. https://jsfiddle.net/planxdesign/ddptkykf/2/
This is my HTML:
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-10 col-md-offset-1">
<div class="row">
<div class="col-sm-4">
<div class="input-wrap">
<div class="heading">
<i class="fa fa-envelope iconic"></i>Home
</div>
<div class="edit2 input">12345</div>
</div>
</div>
<div class="col-sm-4">
<div class="input-wrap">
<div class="heading">
<i class="fa fa-envelope iconic"></i>Home
</div>
<div class="edit2 input">12345</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-3 col-xs-offset-5 col-sm-2 col-sm-offset-6">
<button id="edit-user" class="btn btn-default"><i class="fa fa-pencil-square-o icon"></i>Edit My Details</button>
</div>
</div>
</div>
My JS:
var user2= {
editMyDetailsButton: function()
{
// Get the edit button
$('#edit-user').on('click', function(ev)
{
ev.preventDefault();
user2.editables(); // When click, make it editables
});
},
editables: function()
{
var $edits = $('.edit2'); // All those with '.edit'
$edits.each(function()
{
$context = $(this);
user2.editablesStyle($context); // Make them visibly different
user2.editableClickListeners($context); // Attach click the listeners
user2.editableKeyListener($context); // Attach key listener
});
},
editablesStyle: function($context)
{
// Set contenteditable=true
$context.attr('contenteditable', 'true');
$('.heading').attr('contenteditable', 'false');
// Add them class editable
$context.addClass('editable');
},
editableClickListeners: function($context)
{
$('.input-wrap').on('click', '.edit2, .heading', function( )
{
console.log(this);
// When the editable field is clicked, select all inside
document.execCommand('selectAll', false, null);
});
}
}
user2.editMyDetailsButton();