javascript - how to create an editable input on click event - Stack Overflow

So I have a page that I want to use to allow users to see what in currently in the database and edit th

So I have a page that I want to use to allow users to see what in currently in the database and edit them by clicking on the box. I have a pretty hacky method that works for text imputs but for a drop down box or date selector pletely falls apart. my HTML

   <td name='joineddate133'>
    <input type='date'
    id='joind133'
    name='joinda133
    value='2012-03-15'
    class='toedit'
    readonly='readonly'
    onclick='enablejoindate(this.id)'
    size='20'   />
    < /td>

The current Javascript

<script>
function enablejoindate(joindatid){
    $(function(){ 
    $("input:text[id="+ joindatid +"]").removeAttr("class");
    $("input:text[id="+ joindatid +"]").removeAttr("readonly");
    $("input:text[id="+ joindatid +"]").addClass("inlineditjoind");
  });
  }
  </script>

The inlinedit class is used as a marker so the jquery can find it easily to post and the toedit class currently just hides the attributes.

Obviously this solution isn't great and I would like to try and work out a better way to maybe create an input on the double click function etc.

So I have a page that I want to use to allow users to see what in currently in the database and edit them by clicking on the box. I have a pretty hacky method that works for text imputs but for a drop down box or date selector pletely falls apart. my HTML

   <td name='joineddate133'>
    <input type='date'
    id='joind133'
    name='joinda133
    value='2012-03-15'
    class='toedit'
    readonly='readonly'
    onclick='enablejoindate(this.id)'
    size='20'   />
    < /td>

The current Javascript

<script>
function enablejoindate(joindatid){
    $(function(){ 
    $("input:text[id="+ joindatid +"]").removeAttr("class");
    $("input:text[id="+ joindatid +"]").removeAttr("readonly");
    $("input:text[id="+ joindatid +"]").addClass("inlineditjoind");
  });
  }
  </script>

The inlinedit class is used as a marker so the jquery can find it easily to post and the toedit class currently just hides the attributes.

Obviously this solution isn't great and I would like to try and work out a better way to maybe create an input on the double click function etc.

Share Improve this question asked Sep 30, 2013 at 9:40 alexandercannonalexandercannon 5436 silver badges24 bronze badges 2
  • With a dropdown, how do you envisage a user would add a new menu item? – ChrisW Commented Sep 30, 2013 at 9:47
  • I was thinking it would just exist in place of the text, so it goes from looking like just text to being an input – alexandercannon Commented Sep 30, 2013 at 9:55
Add a ment  | 

4 Answers 4

Reset to default 2

Since you are using jQuery, use a jQuery event. It's best to set readonly to false with the prop() method:

$('input[type=date]').on('click', function(){
    $(this)
      .removeClass("toedit")
      .prop("readonly", false)
      .addClass("inlineditjoind");    
});

JSFiddle

You could have all fields as readonly and hidden until focussed:

$("table").on("focus", "input, select", function(){
    $(this)
    .prop("readonly", false)
    .removeClass("toedit");
});

$("table").on("blur", "input, select", function(){
    $(this)
    .prop("readonly", true)
    .addClass("toedit")
    .siblings("span").text($(this).val());
});

$("table").on("click", "td", function(){
    $(this).children().focus();
});

DEMO (updated again)

You should take a look at X-editable.

<a
  href="#"
  id="joinda133"
  data-type="date"
  data-viewformat="dd.mm.yyyy"
  data-pk="1"
  data-placement="right"
  data-original-title="Date you've joined"
>25.02.2013</a>

Have a look to the DEMO JSFiddle

JS/JQUERY -

$("#joind133").on('click',function(){
    $(this).removeProp("readonly")
        .removeClass("toedit")
        .addClass("inlineditjoind");
});

HTML -

<td name='joineddate133'>
    <input type='date' id='joind133' name='joinda133' value='2012-03-15' class='toedit' readonly='readonly' size='20'/>
</td>

UPDATE ON REQUEST FOR MAKING IT GENERIC BINDING

$("input[type='date']").on('click',function(){
    $(this).removeProp("readonly")
        .removeClass("toedit")
        .addClass("inlineditjoind");
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744313742a4568078.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信