Michael
Michael

Reputation: 171

Chrome and oninput events

I have a textbox, and I am trying to fire an event oninput (my example below only removes a comma from the input, however I do need it to do more advance things). My code works great on Firefox and Internet Explorer, however when I click into the textbox in Chrome, I have about .5 seconds to start typing, otherwise I loose focus. Testing the exact code below on my website creates the error. Any idea?

<input type="text" id="question" name="question" oninput="clean(this);" />

<script type="text/javascript">
    function clean(q){
        q.value=q.value.replace(",","");
    }
</script>

Thanks for any help

Upvotes: 0

Views: 3051

Answers (4)

Rafael Gomes
Rafael Gomes

Reputation: 112

If you are going to do some complex things I'd recommend you use jQuery, because will fix this crossbrowser errors.

and use something like:

$('#input-desired').bind('keyup', function() {
     this.value = this.value.replace(',', '');
});

If you wanna go for a simple application you can try this.

<input type="input" id="desired" onkeyup="desiredInputChanges(this);" />

In your Javascript file use something like:

desiredInputChanges = function (input) {
   input.value = input.value.replace(',', '');
}

Just a note, don't forget to make a function to be your class and protect your methods :)

Upvotes: 0

Michael
Michael

Reputation: 171

Credit goes to ComFreek and RobH for pointing it out that it works just fine on JS fiddle. I feel quite dumb for not trying it first. Turns out that some of the other Javascript on the page was causing the trigger to break. Thanks for everyone's help!

Upvotes: 1

Pieter
Pieter

Reputation: 1833

Try to use <input onkeyup="clean(this)">, it will only fire the function after something is typed into the input.

http://www.w3schools.com/jsref/event_onkeyup.asp

Upvotes: 0

jawerty
jawerty

Reputation: 155

You can try to set a timer to run the code in the function.

<input type="text" id="question" name="question" oninput="clean(this);" />

<script type="text/javascript">
    function clean(q){
        window.setTimeout(function(){
            q.value=q.value.replace(",","");
        }, 0);
    }
</script>

Upvotes: 0

Related Questions