CodeMonkey
CodeMonkey

Reputation: 2295

Updating Input text using jQuery on the fly

I am working on a form using jQuery. In the form i am trying to update a field as soon as user type inputs. Pretty much like, when user type some special characters it removes them and update the text at the same time. I am using jquery for the same. However my code is not working.

Here is what i did:

Form code:

<form name="user">
   <input name="appShortName" type="text" class="form-control" id="inputappShortName" placeholder="Application Name">
</form>

jQuery Code:

$(function(){
   $('#appShortName').keyup(function(){
      $('#appShortName').textreplace(/[^\w]/gi, '');
   });
});

Upvotes: 1

Views: 1227

Answers (5)

Harish Singh
Harish Singh

Reputation: 3329

use #inputappShortName instead of #appShortName

$('#inputappShortName').keyup(function(){
    var newValue = $(this).val().replace(/[^\w]/gi, '')
    $(this).val(newValue);
});

check this fiddle

Upvotes: 2

Ashish Ranjan
Ashish Ranjan

Reputation: 11

You have try this code.

$('#inputappShortName').keyup(function(){
    var newValue = $(this).val().replace(/[^\w]/gi, '')
    $(this).text(newValue);
});

Upvotes: 1

pm.calabrese
pm.calabrese

Reputation: 376

if you want to target the input field without the id you can use this:

$(function(){
    $('input[name="appShortName"]').keyup(function(){
        var newValue = $(this).val().replace(/[^\w]/gi, '')
        $(this).val(newValue);
    });
});

The jsfiddle: http://jsfiddle.net/x65St/

Upvotes: 0

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this,Shorter version

    $(function(){
       $('#inputappShortName').keyup(function(){
           this.value = this.value.textreplace(/[^\w]/gi, '');
       });
    });

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

You need to use val() as a getter, and val('value') as a setter. Try this:

$('#appShortName').keyup(function(){
    var newValue = $(this).val().replace(/[^\w]/gi, '')
    $(this).val(newValue);
});

Also, the id of the attribute in your HTML and the jQuery selector do not match, is that a typo?

Upvotes: 1

Related Questions