0

This is an edit function like in CRUD system, but the idea is to simplify whereby when the user by using single or double click on an item, it will be turned into a field to directly update the data..

Demo here

The problem is I can't type anything into the field, I wonder why...

Html

<li style="list-style:none">click here to edit</li>

jquery

$(document).ready(function(){

$('li').click(function(){
   $(this).html("<input id='input' type='text'>");
});

$('#input').blur(function(){
  var newVal = ('#input').val();
      $('li').text('newVal');   
   });
});
3

3 Answers 3

2

It is because a click on the textfield is also a click on the 'li'. Therefore it adds a new textfield when you click on the existing textfield.

Here is the working fiddle: http://jsfiddle.net/DNmNE/5/

And the code:

$(document).ready(function(){
    $('li').click(function(){
       input = $("<input id='input' type='text'>").blur(function(){
          $('.editable').removeClass('editable');
          var newVal = $(this).val();
          $('li').text(newVal);   
       });
       if (!$(this).hasClass('editable'))
           $(this).addClass('editable').html(input);
    });
    });

I used the .editable class to detect if a textfield is already open.

Sign up to request clarification or add additional context in comments.

5 Comments

Please add also input = $("<input id='input' type='text' value='"+this.innerHMTL+"'>"). ... to your code.
a)input is a variable?
@Stano It was accepted as the correct answer to the question, and seems to be working fine to me. It does work correctly.
@Nurul Yes, input is a variable, you need it to bind the blur event and use it later to set HTML content. I just forgot to write 'var' because I'm used to CoffeeScript ;)
@Parrotmaster Partially, it always creates the empty input element. But it's easy to fix. Ferdinand's answer is great, so of course +1.
1

Well my amigo, you are appending a new input everytime you click so you need to type real fast (just kidding). But thats the problem. loose the li click function.

Comments

0

Just a thought, but have you considered using the HTML5 contenteditable functionality?

http://html5demos.com/contenteditable

Obviously, it's not going to work in obsolete browsers, but if you're not concerned with that then it will save you a lot of work as your application grows.

4 Comments

Why is that an issue? Not why might it be an issue, but why would it be a problem in @Nurul's example?
my whole project is almost in jquery, is there anything that is similar to HTML5's contenteditable can be done in jquery? or a custom logic perhaps?
localstorage is.....local. Only the person editing it can see it, which I doubt is what he wants. Plus it easily gets thrown away by a simple delete of the browsers cache.
True, but the same .blur() functionality can be applied to any element in most modern browsers. A quick AJAX routine can POST the new content to the server :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.