StaticVariable
StaticVariable

Reputation: 5283

How to get last value inserted with javascript

I have got a simple input tag

<input type="text" id="textb" name="textbox"/>

jquery is

$("document").ready(function(){

$("#textb").live("keyup",function(){
alert($(this).val());
//this gives me complete value inserted by user 
})
})

the question is that i want to get only the last value inserted by user if user enter ("hello"),Than it should show alert("o")

Upvotes: 1

Views: 1363

Answers (4)

Decker W Brower
Decker W Brower

Reputation: 941

I recommend putting the output in a div as it is more user friendly.

    $( document ).ready( function() {
        $( 'input' ).keyup( function( evt ) {
            var val = $( this ).val(),
            newVal = val.substr( -1, 1 );

            $( '#output' ).empty();
            $( '#output' ).html( newVal );
        });
    });

or see the user friendly version here http://jsfiddle.net/wh1t3w0lf21/pUNXD/

Upvotes: 0

Liggy
Liggy

Reputation: 1201

You could update to this:

alert($(this).val().substr($(this).val().length-1));

Upvotes: 1

Ram
Ram

Reputation: 144689

try this:

$("#textb").live("keyup",function(){
    alert($(this).val().slice(-1));
})

http://jsfiddle.net/V3RAF/

or:

$("#textb").on("blur",function(){
    alert($(this).val().slice(-1));
})

http://jsfiddle.net/V3RAF/1/

Upvotes: 2

GTSouza
GTSouza

Reputation: 365

jQuery("document").ready(function($){
    $("input").bind("keyup",function(e){
        alert(String.fromCharCode(e.keyCode));
    });
});

Upvotes: 2

Related Questions