Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:02:37+00:00 2026-05-27T02:02:37+00:00

I’m trying to do an input that only accept digit. I can have some

  • 0

I’m trying to do an input that only accept digit. I can have some dynamic input so I use the .live function.

On my .live('keyup') function I’m adding some “,” which depends of the input length.
Here’s a part of my code on an input (not dynamic):

 $(document).ready(function(){
      $('#test').change(function() {
           alert('trigger change');
      });
 });


 $('#test').live('keyup', function() {
    var realDotPos = $(this).val().indexOf('.');
    var inputValue = '';
    var inputDecimals = '';
    var value = '';
    var length = 0;
    var cptMod = 0;
    var cptVirgule = 0;

    if (realDotPos == -1){
        inputValue = $(this).val().replace(/,/g, '');
    } else{
        inputValue = $(this).val().substring(0,realDotPos).replace(/,/g, '');
        inputDecimals = $(this).val().substring(realDotPos);
    }

    length = inputValue.length;
    if (length > 3){
        for(i=length-1; i>=0; --i){
            if ((cptMod != 0) && (cptMod % 3 == 0)){
                value += ',' + inputValue[i];
                ++cptVirgule;
            } else{
                value += inputValue[i];
            }
            ++cptMod;
        }
        value = value.split('').reverse().join('');
        $(this).val(value + inputDecimals);

    } else{
        $(this).val(inputValue + inputDecimals);
    }

});

The problem with this is that in ie, the .change function is not triggered when i click out of the input. FF or chrome works fine. If i remove the line $(this).val(inputValue + inputDecimals); the trigger works. What’s wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:02:38+00:00Added an answer on May 27, 2026 at 2:02 am

    I’ve created this jsfiddle to test your code.

    The change event does not fire in chrome for me, neither as in IE.

    What you can do is trigger the change event by calling .change():

    $(this).val(value + inputDecimals).change(); // or with .trigger('change')
    


    Event delegation:

    .live() has been deprecated in newer version of jQuery, you should look for .on() (in case you don’t use the very latest version neither, check for .delegate())

    Using .on() with a selector will delegate the event handling and thus handle any element (represented by the selector) inserted in the dom after it is initialized.

    So here I delegate to the document, the events change/keyup for all inner element which has a class ‘.myinput’:

    I’ve made a fiddle for you to show the updated code, and how it works when adding dynmically new inputs: http://jsfiddle.net/didierg/xw7SV/9/

    Here’s the updated js code (with the optimizations and event delegation):

    $(document).ready(function() {
    
        $(document).on('change', '.myinput', function(e) {
            alert('trigger change');
        });
    
        $('#test').on('keyup', '.myinput', function(e) {
    
            var $this = $(this),  // select 'this' only once
                currval =$this.val(),  // keep the initial value to work with
                newvalue = null,
                realDotPos = currval.indexOf('.'),
                inputValue = '',
                inputDecimals = '',
                value = '',
                length = 0,
                cptMod = 0,
                cptVirgule = 0;
    
            if (realDotPos == -1) {
                inputValue = currval.replace(/,/g, '');
            } else {
                inputValue = currval.substring(0,realDotPos).replace(/,/g, '');
                inputDecimals =currval.substring(realDotPos);
            }
    
            length = inputValue.length;
    
            if (length > 3) {
    
                for(i=length-1; i>=0; --i) {
                    if ((cptMod != 0) && (cptMod % 3 == 0)){
                        value += ',' + inputValue[i];
                        ++cptVirgule;
                    } else{
                        value += inputValue[i];
                    }
                    ++cptMod;
                }
    
                value = value.split('').reverse().join('');
    
                newvalue = value + inputDecimals;
    
            } else {
                newvalue = inputValue + inputDecimals;
            }
    
            $this.val(newvalue).change(); // call .change() to trigger the event
    
        });
    
    });
    

    Some notes about your code:

    • You should save your selections: var $this = $(this);

    • As well as the value, get it once: var val = $this.val()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.