0

I met some trouble with a javascript.

In fact I have in my database many records that are abreviations and ther equivalent,

for example, replace tel => telephone etc...

So I have this function

$('#tags').keyup(function(e){
    var code = e.which ? e.which : e.keyCode;
     console.log(code);
    if (code == 'tel'){
        var input = this.value;
        input = input.substr(0, input.length -1);
        console.log(input);
        input += 'tel';
        this.value = input;        
    }

});

Actualy this does not work the trouble is that I do not have aby mistakes in the console of javascript.

Anykind of help will be much appreciated.

Kind regards.

SP.

Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59

5 Answers5

7

This should work:

$('#tags').keyup(function(e){
    var code = e.which ? e.which : e.keyCode;
    var input = this.value;
    if (input.indexOf('tel') != -1) {
       this.value = this.value.replace(/\btel\b/gi,'telephone');

    }

});

Here is a fiddle

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
  • This wil change only the literal "tel", not if it's enclosed in a string like "this is my tel" – Christiaan Nieuwlaat Oct 31 '12 at 08:56
  • dear sir, thanks for you reply I've done a php code that do a while for all words to be replaced. The trouble is that actualy it add a ? at the end of the script and this function do not work. I really do not know where does this ? comes from. Is there a way to remove that? Kind Regards.? SP – Stanislas Piotrowski Oct 31 '12 at 09:11
  • Are you referring to this : http://stackoverflow.com/questions/11988482/jsfiddle-code-not-working-on-local-machine/11988599#11988599 – Pranav 웃 Oct 31 '12 at 09:19
  • Oh thanks, This code works fine on the fiddle but not in my page, do I added this jQuery(function($){ and a } at the end, but it still does not work – Stanislas Piotrowski Oct 31 '12 at 09:29
  • @StanislasPiotrowski: `jQuery((function(){});` should work just fine, to ensure the `$` sign to work: `jQuery((function($){return function(){/* your code here*/};}(jQuery));` is what you're after. Mind you, this answer will change _visit Tel-Aviv_ to `visit telephone-Aviv`. I've updated my answer to a slightly more complicated but a lot more flexible sollution – Elias Van Ootegem Oct 31 '12 at 14:36
3

When using keyup() the event handler only returns the keycode of the pressed key. For instance an x results in e = 88.

Use $("#tags").val() to get the value of the input element.

Johannes Mittendorfer
  • 1,102
  • 10
  • 17
  • so there is no way to replace a string on keypress? – Stanislas Piotrowski Oct 31 '12 at 08:51
  • 1
    Sure, there is, but the primary problem was that the code will never execute that command, as `code` will never be a string. Use `str.replace` for the replacement. – Johannes Mittendorfer Oct 31 '12 at 08:52
  • don't use `$('#tags')` in the handler, use either `$(this).val()`, `$(this)[0].value` or `this.value`. Using a jQuery selector will scan the DOM, which is slower and completely unnecessary, because you have a reference to the element: `this` – Elias Van Ootegem Oct 31 '12 at 09:01
3

the keyCode or which property doesn't return a string, or even a single char. It returns the key code that represents the key that was struck by the client. If you want to get the corresponding char: String.fromCharCode(e.which || e.keyCode);.
If the user hit on the a key, for example, the keycode will be 97, String.fromCharCode(97) returns a.

If you want to know weather or not the current value of the element contains the abreviation: tel, what you'll need to do is this:

$('#tags').keyup(function(e)
{
    this.value = this.value.replace(/\btel\b/gi,'telephone');
});

This is untested and very likely to need some more work, but AFAIK, you want to replace all occurrences of tel by telephone. The expression I use /\btel\b/ replaces all substrings tel, provided they are preceded and followed by a word-boundary (to avoid replacing part of a word).
Not that the end of a string and a dash are both considered to be word boundaries, too. If I wanted to type television, I'd end up typing telephoneevision. To avoid this, you'll need a slightly more complex expression. here's an example how you can avoid JS from treating a dash as a boundary, just work on it to take string-endings into account, too

Update

Perhaps this expression isn't quite as easy as I thought, so here's what I'd suggest you use:

this.value = this.value.replace(/(?:(\-?\b))tel(?:(\b\-?.))/gi,function(all,b1,b2)
{
    if (b1 === '-' || b2.charAt(0) === '-')
    {//dash, don't replace
        return all;
    }//replace
    return b1 + 'telephone' + b2;
});

Input: I need a tel, quickly ==> I need a telephone, quickly
I need a tel ==> I need a tel (if the user is still typing, don't replace, he could be typing telescope, replace on submit or on blur)
I want to book a hostel for tonight ==> I want to book a hostel for tonight
Visit Tel-Aviv ==> Visit Tel-Aviv

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

When using keypress this way the code variable will contain the character code of the pressed character. Not the string of chars like the expected 'tel'. You could use onkeyup / onchange event and check the val() of the input element and use replace() to change the abbreviation to the intended string.

$('#tags').keyup(function(e){

    var elem = $(this);
        var input = elem.val();

        // input = input.substr(0, input.length -1);  // This might not be necessary
        console.log(input);

        // do the replacement
        input = input.replace('tel','telephone');

        elem.val(input);        
    }

});
  • This will only replace the first occurance of `tel`, over and over again. If you type `tel and bell` the result will be something like `telephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephonetelephoneephone and bell` – Elias Van Ootegem Oct 31 '12 at 08:58
  • I know this wil change every instance of "tel", so "hostel" will become "hostelephone".. When using regex replace(/\stel\s|^tel\s|\stel$/i, 'telephone') this would be fixed.. ;) .. and I've just learned something ( the \b in regex ) myself thanks to Elias :) – Christiaan Nieuwlaat Oct 31 '12 at 08:59
  • 1
    I'd not use `tel$` in my expression, like I said in my answer: client won't be able to type `I want to by a television` because, at some point, the input will be `I want to by a tel` --> matches `\stel$` and will be replaced – Elias Van Ootegem Oct 31 '12 at 09:05
0

Use replace method for replacing a word in string.

eg:

var str = "This is AIR";
var newstr = str.replace("AIR", "All India Radio");
console.log(newstr);
Quicksilver
  • 2,546
  • 3
  • 23
  • 37