Moncho Chavez
Moncho Chavez

Reputation: 694

Cant use arrows inside input

Hi i did not make this script, but its a posting form, simple just html,

This is the html in input

<input class="text_box" type="text" name="title" id="title" size="19" value="Title"  onkeydown="SpecialReplace(this)" onkeyup="SpecialReplace(this)" onblur="SpecialReplace(this)" onclick="SpecialReplace(this)"/>

And this is the SpecialReplace() function

function SpecialReplace(o)
{
      o.value=o.value.replace(/[^a-zA-Z0-9 áéíóúÁÉÍÓÚÜüñѨ´,.¿?%&$!¡ªº#"()-_\/]/g,'');
}

I try to use arrows, to go to a specific letter to "edit" but i cant use the arrows in the input, why??

and how can i solve this?

Upvotes: 0

Views: 86

Answers (1)

plalx
plalx

Reputation: 43728

Why all these inline handlers if you are using jQuery?

Anyway, here's how you could do it:

HTML

<input class="text_box" type="text" name="title" id="title" size="19" value="Title">

JS

$(function () {
    //you have to escaped the - character in a character class
    var cleanRx = /[^a-zA-Z0-9 áéíóúÁÉÍÓÚÜüñѨ´,.¿?%&$!¡ªº#"()\-_\/]/g;

    $('#title').keyup(function (e) {
        var which = e.which;

        //avoid useless replacements when <- and -> keys are pressed
        if (which === 39 || which === 37) return;

        this.value = this.value.replace(cleanRx, '');

    }).trigger('keyup'); //perform replacement on initial content (remove if uneeded)

});

Upvotes: 1

Related Questions