The Virtual keyboard and the slow loading jQuery - javascript

I tried to add a virtual keyboard to my code using jQuery, in fact I have several text fields and I added to each on this keyboard.
My problem is when I load the page it take a lot of times (~ 10 seconds) to display.
This is my Demo.
Code:
var table = document.getElementById("Table-1");
var rowCount = table.rows.length;
for(var i=0;i<150;i++) {
row = table.insertRow(rowCount);
cell1 = row.insertCell(0);
cell1.name = "animate";
cell1.id = i ;
var values = document.createElement("input");
values.type = "text" ;
cell1.appendChild(values);
rowCount++;
}
$('input[type=text]').keyboard({
layout: "qwerty"
});
​

A first solution to my problem ; I only call the keyboard when I click in the text box.
Demo.
But it's still a bit slow...
Code :
var table = document.getElementById("Table-1");
var rowCount = table.rows.length;
for(var i=0;i<150;i++) {
row = table.insertRow(rowCount);
cell1 = row.insertCell(0);
cell1.name = "animate";
cell1.id = i ;
var values = document.createElement("input");
values.type = "text" ;
cell1.appendChild(values);
rowCount++;
$('#'+i).click(function() {
$('input[type=text]').keyboard({
layout: "qwerty"
});
});
}

Related

Can I create multiple onclick events dynamically?

For some reason I am unable to re-enable a button and go through all of the columns I have in a table with the same button. Can I create multiple onclick events for the button? The HTML itself is really just a button within a table.
function focaccia(x){
var table = document.getElementById("orderTable");
var row = table.insertRow(0);
var rem = document.createElement("button");
var name = document.createTextNode("Remove");
rem.appendChild(name);
rem.onclick = function(){
x.disabled = false;
document.getElementById("orderTable").deleteRow(this);
var table = document.getElementById("orderTable");
var amount = 0;
var tot;
for(var x = 0; x < table.rows.length; x++){
amount = amount + parseInt(table.rows[x].cells[1].children[0].value);
}
tot = amount * 9;
document.getTotal.total.value = tot;
}
var num = document.createElement("input");
num.size = 2;
num.onchange = function(){
var table = document.getElementById("orderTable");
var amount = 0;
var tot;
for(var x = 0; x < table.rows.length; x++){
amount = amount + parseInt(table.rows[x].cells[1].children[0].value);
}
tot = amount * 9;
document.getTotal.total.value = tot;
}
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = "Focaccia";
cell2.appendChild(num);
cell3.appendChild(rem);
x.disabled = true;
num.value = 1;
total = total + 9;
document.getTotal.total.value = total;
}
Yes, use addEventListener.
Instead of target.onclick=clickHandlerFunction;, use target.addEventListener('click', clickHandlerFunction);'
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener
Note: The syntax for adding events using old Internet Explorer is different (target.attachEvent('onclick',handler)). If you need compatibility with old browsers, you can use both syntaxes, use jQuery's click or on functions or gator.js.

Checkbox checked property is undefined

I am adding rows and cols to html table programatically using Javascript:
for (var i = 0; i < results.length; i++) {
table = document.getElementById("EntityRecords");
rowCount = table.rows.length;
row = table.insertRow(rowCount);
row.id = results[i].LeadId;
row.className = "EntityRecordsRow";
cell1 = row.insertCell(0);
element = document.createElement("input");
element.type = "checkbox";
element.name = "chkbox[]";
cell1.appendChild(element);
cell1.className = "EntityRecordsCol";
cell2 = row.insertCell(1);
cell2.innerHTML = results[i].FirstName + " " + results[i].LastName;
}
On button click event, I need to find selected checkbox. I am reading the table cells using below code but checkbox cell checked property is undefined:
function OnRecordSelectionBtnClick() {
var table = document.getElementById("EntityRecords");
for (var i = 0, row; row = table.rows[i]; i++) {
var col = row.cells[0];
if (col.checked) {
var selectedText = row.cells[1].innerHTML;
alert(selectedText);
}
}
}
Any suggestions?
if ( col.firstElementChild.checked ), not if ( col.checked ). col is td element and its first child is input that has checked property.

Datepicker not working in dynamically added row

When a row is added dynamically in a table, I don't get the datepicker,time spinner or the validations working for the second row.
my code for adding a row...
function addRow(tableID){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i = 0;i<colCount;i++){
var newccell = row.insertCell(i);
newcell.innerHTML = table.rows[i].cells[i].innerHTML;
}
}
and my code for the button is
<input type = "button" onclick = "addRow(tableID)" name="+" value="+" id="add">
You need to initialize the plugins/widgets for dynamic elements once the elements are rendered to the dom
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newccell = row.insertCell(i);
newcell.innerHTML = table.rows[i].cells[i].innerHTML;
}
var $row = $(row);
$row.find('input.spinner').spinner(){};//initialize spinner
$row.find('input.datepicker').datepicker({});//iniitailze datepicker etc
}
use clone
function addRow(tableID) {
var $table = $('#' + tableID),
$first = $table.find('tr').first();
var $row = $first.clone().appendTo($table)
$row.find('input.spinner').spinner() {};
$row.find('input.datepicker').datepicker({});
}
You need .on()
As elements are added dynamically you can not bind events directly to them .So you have to use Event Delegation.
Syntax
$( elements ).on( events, selector, data, handler );
Add validation code using Event Delegation.
Update
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newccell = row.insertCell(i);
newcell.innerHTML = table.rows[i].cells[i].innerHTML;
}
var $row = $(row);
$row.find('input.sp').spinner(); //find element which you want to add spinner
$row.find('input.dp').datepicker();//find element which you want to add datepicker
}

Add and removing rows [with input field] in a table

I'm a total javascript noobie. I developed the code bellow following and modifing some random tutorial I found.
It should add and remove rows with input fields at a table, however, it does nothing. It also worth saying that I called the function as a link. I added 'javascript:addRow()' inside the tag and at the header. Did I missed something?
function addRow(){
tableID="ticketCreate";
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount<7){
var row = table.insertRow(rowCount);
var cel1=row.insertCell(0);
var element1= document.createElement("input");
var element1.type="text";
cell1.appendChild(element1);
var cell2=row.insertCell(1);
var element2.type="text";
cell1.appendChild(element2);
var cell2=row.insertCell(2);
var element3.type="text";
cell1.appendChild(element3);
rowCount++;
}
}
function removeRow(){
tableID="ticketCreate";
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount>1){
table.deletRow(rowCount);
rowCount--;
}
}
You have several errors, but here is the basic working model. I think you should be able to sort it out from here
http://jsfiddle.net/dBzkX/
function addRow() {
var tableID="ticketCreate";
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount<7){
//You declared var in front of the same variable twice. Don't do that.
//You were appending cells inside existing cell. Add them to row instead.
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement('input');
element1.type="text";
cell1.appendChild(element1);
row.insertCell(1);
row.insertCell(2);
}
}
function removeRow(){
var tableID="ticketCreate";
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount>1){
//you had type in deletRow. Also, you can pass in -1 to remove the last row
table.deleteRow(-1);
}
}
The error is here:
var element1= document.createElement("input");
var element1.type="text";
It should be
var element1= document.createElement("input");
element1.type="text";
and similar for the other elements.
You declare the variable with
var element1 = 'something';
and then access it with
element1 = 'something different';
Also there is a typo in
var cel1=row.insertCell(0);
It needs to be
var cell1=row.insertCell(0);
Additionally you did not define element 2 and 3,
used cell2 twice where it should be cell2 and cell3 and only appended to cell1.

Adding Combo-box dynamically

I'm trying to design a table in HTML which can add a row when I click "add row" button & the same for deleting an added row if i intend to delete it. I have written a javascript for adding checkbox and text. But I want even combo-boxes in it and I m stuck in the middle. Could you guys just figure it out and let me know how to do that?
This is my JS file.
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
cell3.appendChild(element2);
var cell4 = row.insertCell(3);
var element3 = document.createElement("input");
element3.type = "text";
cell4.appendChild(element3);
var cell5 = row.insertCell(4);
//This is where the PROBLEM is!!
var element4 = document.createElement("select");
element4.type = "option";
cell5.appendChild(element4);
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
// JavaScript Document
NOTE: Please dont suggest SERVER_SIDE SCRIPTING. I'm just doing my homework on Java Script :)
This should do the trick:
var cell5 = row.insertCell(4);
//This is where the SOLUTION is!!
var element4 = document.createElement("select");
var option1 = document.createElement("option");
option1.value="1";
option1.innerHTML="sample1";
element4.appendChild(option1);
var option2 = document.createElement("option");
option2.value="2";
option2.innerHTML="sample2";
element4.appendChild(option2);
cell5.appendChild(element4);
Try to think of the outcome that you wish to get. In this case you wish to have HTML that looks like this:
<select>
<option></option>
<option></option>
</select>
So the question is what elements are there? There are three in my example, a select and two options. So in your JavaScript how do you create elements?
var element4 = document.createElement("select");
This creates a select. So how would you create an option?
var option1 = document.createElement("option");
maybe?
how would you add the option to the select? Same way you add the select to the cell.
element4.appendChild(option1);
then create the other options that you need and add the select to the cell.
why not trying
var sel=document.createElement("select");
// repeat this for each option you have
var opt=document.createElement("option");
opt.value="my option value";
opt.text="my option to be displayed";
sel.appendChild(opt);
// end repeat
cell5.appendChild(sel);

Categories