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 pares 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!
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 pares 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!
- 2 You're not assigning the value to anything. – Prinzhorn Commented Mar 18, 2014 at 12:59
1 Answer
Reset to default 7You 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;