Search code examples
jquerytwitter-bootstrapx-editable

taking decimal inputs for x-editable field


I am using x-editable with bootstrap

I have the following html field defined

<div class="form-group">
    <label class="col-sm-4">Service Charges(%)</label>
    <div class="col-sm-6 hoverEdit">
        <a class="editableAccount" id="serviceTax"></a>
    </div>
</div>

I have the following script defined to make the field editable

$('.editableAccount').editable({
    type : 'number',
    title : 'Enter New Value',
    success : function(response, newValue) {
        updateAccount(this.id, newValue);
    }
});

Now when i try to edit the field and enter decimal values i get this

enter image description here

I want to make this field editable so that i can enter decimal values. I can't see any help on the documentation. Some one please help me here, i am fairly new to html and javascript.


Solution

  • UPDATE (Nov 1, 2018): Following a recent downvote, I came back to review my answer, and here is what I found out: selection is used by x-editable to process the field to be edited, but selection is only supposed to work on fields rendered as text, thus using x-editable with a number field will either produce an error or just do nothing (see chrome bug 324360 and 415391 for details). So use one of the other answers provided in this question, or see x-editor's github issue #328 with similar solutions.

    Here is my original answer for historical purposes:


    Inputs of type number need a step attribute for them to accept decimals.

    Source: https://www.w3.org/TR/2010/WD-html-markup-20101019/input.number.html#input.number.attrs.step.float

    Going out from that, you'll most probably need to use your jQuery call like this:

    $('.editableAccount').editable({
        type : 'number',
        step: 'any', // <-- added this line
        title : 'Enter New Value',
        success : function(response, newValue) {
            updateAccount(this.id, newValue);
        }
    });
    

    Note: I haven't tested this, it's just the best logical move I think

    EDIT: Following your comment (thanks!), looks like my suggestion is not working because of a limitation recently introduced in chrome (not sure about other browsers) to number fields no longer allowing selection (source: selectionStart and SelectionEnd incorrectly available on <input type=email/number>). So what you could try instead, is to change the field type to something else and do your number validation in your updateAccount function instead.