Please someone help me convert this jQuery function into a JavaScript onkeyup
or onchange
function. I'm always getting an error undefined when I try to alter it as a JavaScript.
Your help will be deeply appreciated.
var inp = $("#txt");
var tbl = document.getElementById("myTable");
// where #txt is the id of the textbox
inp.keyup(function (event) {
if (event.keyCode == 13) {
if (inp.val().length > 0) {
var trow = document.createElement('tr');
var tdata_type = document.createElement('td');
var tdata_code = document.createElement('td');
tdata_type.textContent = $("#select option:selected").text();
tdata_code.textContent = inp.val();
trow.appendChild(tdata_code);
trow.appendChild(tdata_type);
tbl.insertBefore(trow,tbl.firstChild);
}else{
alert("Barcode length insufficient");
}
inp.val('');
}
});
Tried this but I got errors. (index):86 Uncaught ReferenceError: barcode is not defined at HTMLInputElement.onkeyup
<input type="text" name="yes" id="txt" onkeyup="barcode()">
function barcode(){
var inp = $("#txt");
var tbl = document.getElementById("myTable");
if (event.keyCode == 13) {
if (inp.val().length > 0) {
var trow = document.createElement('tr');
var tdata_type = document.createElement('td');
var tdata_code = document.createElement('td');
tdata_type.textContent = $("#select option:selected").text();
tdata_code.textContent = inp.val();
trow.appendChild(tdata_code);
trow.appendChild(tdata_type);
tbl.insertBefore(trow,tbl.firstChild);
}else{
alert("Barcode length insufficient");
}
inp.val('');
}
}
See here to full tried code: http://jsfiddle.net/sLzsweyd/