user2039789
user2039789

Reputation: 97

Remove last character on input field jQuery

I know this kind of question has been asked several times but none of the answers worked for me. I want to make my own spell-checker which compares words letter by letter and checks the correctness. As user types letters one by one, the content is checked. If it is correct, goes on, if it is wrong: I want the last letter to be deleted.

Here is my code:

var word = [];
word = "beautiful".split('');
var i =0;

$(document).ready(
function() 
{ $('#txtword').keyup(
 function() {
    var value = document.getElementById('txtword').value;
    if (value[i] == word[i]){ 
            alert("Right letter!");
            i++; }
            else
            { alert("Wrong letter");
            value.slice(0,-1);
            /*value.substr(0,value.length-1);*/
            }
        });
    })

I tried both options value.slice(0,-1); and value.substr(0,value.length-1); but they are not working. Can someone help me find the mistake!

Upvotes: 3

Views: 5075

Answers (1)

Adil
Adil

Reputation: 148120

You need to assign new value back to value property of the element

this.value = value.substr(0,value.length-1);

Note: You can use this.value instead of document.getElementById('txtword').value;

Upvotes: 7

Related Questions