I am working with Javascript and based of a radio select it changes the fields in a form. The fields are generated with the Javascript. That part is operating very nicely however one of the fields is also a date that I am trying to use the jQueryUI datepicker with. For some reason it is not running the Javascript on a Javascript created field. I'm not sure if it is my order the scripts are in or what but any help would be appreciated. Here is a JSFiddle with the code I am using. It isn't running because I don't have all of the dependencies linked but you get an overview of how I have it set up. I can put more time putting in the dependencies if you feel it's needed or a link to my page.
* I was playing around with it and noticed that when I initially click on the date select it doesn't do anything but when I click another field and go back to it, it then works?
Right now I am focusing on the field that becomes available in the javascript when Pick up is selected which is on line 22 of the Javascript.
Thanks guys!
http://jsfiddle.net/hn9Qe/
Javascript
function show(x)
{
var element=document.getElementById(x.id);
if(element.id=='a')
{
var table = document.getElementById("myTable");
var length = table.rows.length;
if(length > 9)
{
var del1 = table.deleteRow(5);
var del2 = table.deleteRow(5);
var del3 = table.deleteRow(5);
}
var row = table.insertRow(5);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var row2 = table.insertRow(6);
var cell3 = row2.insertCell(0);
var cell4 = row2.insertCell(1);
cell1.innerHTML = "Pick-Up Date:";
//For right now just focusing on getting this datepicker to properlly popup.
cell2.innerHTML = "<input type='text' name='datepicker' id='datepicker' onFocus=dateSelect()>";
cell3.innerHTML = "Pick-Up Time:";
cell4.innerHTML = "<input type='text' name='txtTime' id='txtTime'>";
}
else
{
var table = document.getElementById("myTable");
var length = table.rows.length;
if(length > 9)
{
var del1 = table.deleteRow(5);
var del2 = table.deleteRow(5);
}
var row = table.insertRow(5);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var row2 = table.insertRow(6);
var cell3 = row2.insertCell(0);
var cell4 = row2.insertCell(1);
var row3 = table.insertRow(7);
var cell5 = row3.insertCell(0);
var cell6 = row3.insertCell(1);
cell1.innerHTML = "Delivery Date:";
cell2.innerHTML = "<input type='text' name='txtDelDate' id='txtDelDate'>";
cell3.innerHTML = "Delivery Time:";
cell4.innerHTML = "<input type='text' name='txtDelTime' id='txtDelTime'>";
cell5.innerHTML = "Delivery Address:";
cell6.innerHTML = "<input type='text' name='txtDelTime2' id='txtDelTime2'>";
}
}
function dateSelect()
$(function() {
$( "#datepicker" ).datepicker();
});
HTML Form
<form action="cateringSubmit" method="post">
<table id="myTable" width="600" border="0">
<tr>
<td>Company Name</td>
<td><span id="sprytextfield1">
<input type="text" name="datepicker2" id="datepicker2">
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>Contact Name</td>
<td><span id="sprytextfield2">
<input type="text" name="txtConName" id="txtConName">
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>Contact Phone Number</td>
<td><span id="sprytextfield3">
<input type="text" name="txtPhone" id="txtPhone">
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>Email Address</td>
<td><span id="sprytextfield4">
<input type="text" name="txtEmail" id="txtEmail">
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>Pick-Up or Delivery</td>
<td><input name="radioButton" type="radio" id="a" onclick="show(a)">Pick-Up
<input type="radio" onclick="show(this)" name="radioButton" id="b" >Delivery</td>
</tr>
<tr>
<td>Order:</td>
<td><textarea name="txtOrder" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td>Special Pricing (if applicable): </td>
<td><input name="txtPricing" type="text"></td>
</tr>
<tr>
<td>Special Requests: </td>
<td><textarea name="txtRequests" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="btnSubmit" type="submit" value="Submit" ></td>
</tr>
</table>
</form>
</p>
Since those textbox 's are created dynamically you have to attach those event to document like
$(document).on('focus', '.datepicker', function () {
$(this).datepicker();
});
Also since you're using 2 datepicker I have used class selector for those elements.
JSFiddle
The JSFiddle is failing for a few reasons:
jQuery library isn't being loaded
Missing function brackets for dateSelect()
Use of inline onClick and onFocus attributes
I would suggest you move to using jQuery's click() and focus() methods to detect click and focus events for your radio buttons. Hopefully this fixes your issue.
Related
how to create textbox/text field and button
JavaScript
<script type="text/javascript">
function addItemTodo(){
var value = document.getElementById('item1').value;
var value2 = document.getElementById('item2').value;
var my_table = document.getElementById('todo');
var rows = my_table.getElementsByTagName("tr").length;
row = my_table.insertRow(rows);
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell1.innerHTML = value;
cell2.innerHTML = value2;
}
</script>
HTML Code
<input type="text" id="item1" placeholder="item1" size="12">
<input type="text" id="item2" placeholder="item2" size="14">
<button id="add" onclick="addItemTodo()">Add</button>
<table border="1" style="width:50%" id="todo">
<tr>
<th>Input from 'item1'</th>
<th>Input from 'item2'</th>
<th>Text Box</th>
<th>Button</th>
</tr>
</table>
How to append the text box and button, when clicked the add button
You could create your new elements within your Javascript as strings, and then add them to two new cells (insertCell(2) and insertCell(3)) using .innerHTML like so:
function addItemTodo() {
var value = document.getElementById('item1').value;
var value2 = document.getElementById('item2').value;
var my_table = document.getElementById('todo');
var rows = my_table.getElementsByTagName("tr").length;
var row = my_table.insertRow(rows);
row.insertCell(0).innerHTML = value;
row.insertCell(1).innerHTML = value2;
row.insertCell(2).innerHTML = "<input type='text' placeholder='xxx' class='text-box' />";
row.insertCell(3).innerHTML = "<button>Text</button>";
}
.text-box {
width: 50px;
}
<input type="text" id="item1" placeholder="item1" size="12">
<input type="text" id="item2" placeholder="item2" size="14">
<button id="add" onclick="addItemTodo()">Add</button>
<table border="1" style="width:50%" id="todo">
<tr>
<th>Input from 'item1'</th>
<th>Input from 'item2'</th>
<th>Text Box</th>
<th>Button</th>
</tr>
</table>
My problem is I am creating dynamic HTML table by using javascript.
How do I save this dynamic HTML table into a DataBase using CodeIginter?
This my input text code:
<table id="tb3" name="tb3">
<tr>
<td>Product Code</td>
<td>Product Name</td>
<td>Qty</td>
<td>Rate</td>
<td>Amount</td>
</tr>
<tr>
<td>
<input type="text" onkeyup="autofill()" id="Product_Code" name="Prdtcode" class="form-control input-xs Product_Code " required>
</td>
<td ><input type="text" id="Product_Name" name="Prdtname" class="form-control input-xs"></td>
<td><input type="text"
onkeypress="javascript:doit_onkeypress(event)" id="Qty" onkeyup="movetoNext(this, 'addMore3')" name="Qty"class="form-control input-xs" required>
</td>
<td><input type="text" id="Rate" class="form-control input-xs"name="Rate" value="" ></td>
<td><input type="text" id="Value" name="Value" class="form-control input-xs" ></td>
</tr>
</table>
This code gets data from a TextBox and displays in table format using javascript:
<table class="table table-bordered table-striped table-xxs" id="tb2" name="tb2">
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Qty</th>
<th>Rate</th>
<th>Amount</th>
</tr>
This is the javascript code to create table
function doit_onkeypress(event)
{
if (event.keyCode == 13 || event.which == 13){
if(!checkEmptyInput()){
var newRow = table.insertRow(table.length),
cell1 = newRow.insertCell(0),
cell2 = newRow.insertCell(1),
cell3 = newRow.insertCell(2),
cell4 = newRow.insertCell(3),
cell5 = newRow.insertCell(4),
code = document.getElementById("Product_Code").value,
name = document.getElementById("Product_Name").value,
qty = document.getElementById("Qty").value,
rate = document.getElementById("Rate").value,
amt = document.getElementById("Value").value;
cell1.innerHTML = code;
cell2.innerHTML = name;
cell3.innerHTML = qty;
cell4.innerHTML = rate;
cell5.innerHTML = amt;
var prdtcode = $("#Product_Code").val("");
var Prdtname = $("#Product_Name").val("");
var qty = $("#Qty").val("");
var Rate = $("#Rate").val("");
var amt = $("#Value").val("");
}
}
}
My problem is I am creating a dynamic HTML table by using javascript,how I save this dynamic html table into database using codeiginter.
Store whole table html data into one variable
var table_data = $("#table_id").html();
$.trim(table_data. replace(/>[\n\t ]+</g, "><"));
Now Send this table_data variable to controller function to add data
Make sure the datatype of your field should be text or longtext then normally do the insertdata query in controller's function.
The ajax code is as below:
$.ajax({
url:"path to your controller function",
type: 'POST',
data:{
table_data:table_data,
},
success:function(result){
console.log(result);
},
});
The controller's function code is as below:
function insert_table_data(){
$data = array('table_data' => $this->input->post('table_data'));
$this->db->insert('table_name', $data);
}
I want to use insertRow() function for adding rows in my table. but can I insert a row which contain <input> tag. because my table look like this
<table><thead>
<tr>
<th>No Account</th>
<th>Deskripsi</th>
<th>Debit</th>
<th>Credit</th>
<th>Job</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="input-td" type="text" placeholder="No Account">
</td>
<td><input class="input-td" type="text" placeholder="-"></td>
<td><input class="input-td" type="text" placeholder="-"></td>
<td><input class="input-td" type="text" placeholder="0"></td>
<td><input class="input-td" type="text" placeholder="0"></td>
</tr></thead>
</table>
<i class="fa fa-plus"></i>  Add row
so far i could only find this example for the javascript code
function myCreateFunction() {
var table = document.getElementById("bootstrap-data-table");
var row = table.insertRow(3);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = "0";
cell2.innerHTML = "0";
cell3.innerHTML = "0";
cell4.innerHTML = "0";
cell.innerHTML = "0";
}
could I change "0" which is only text to being input tag like the previous row?
You could just assign an HTML string:
cell1.innerHTML = `<input class="input-td" type="text" placeholder="No Account">`;
Other options include creating the DOM via document.createElement and the like.
const input1 = document.createElement("input");
input1.classList.add("input-td");
input1.type = "text";
input1.placeholder = "No Account";
cell1.appendChild(input1);
I have been looking all around through numerous sources to try and find a way to add name="" to generated table rows. Here is the table part of the form being submitted.
<table id="quote_form_table"border="1" style="border-collapse:collapse;border:1px solid #000000;width:100%" cellpadding="3" cellspacing="3">
<tr>
<td>Quantity</td>
<td>Item</td>
<td>Unit Price</td>
</tr>
<tr name="row[]">
<td><input type="text" name="quantity" ></td>
<td>
<select name="products">
<option>Roller banner</option>
<option>Grasshopper</option>
<option>Pop up display</option>
</select>
</td>
<td>
<select name="type">
<option>Roller banner</option>
<option>Grasshopper</option>
<option>Pop up display</option>
</select>
</td>
</tr>
</table>
<br>
<button type="button"onclick="addrow()">Add Row</button>
<button type="button"onclick="removerow()">Remove Row</button><br><br><br>
Here is the script adding and removing rows.
<script>
function addrow() {
var table = document.getElementById("quote_form_table");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = '<input type="text" name="quantity" >';
cell2.innerHTML = '<select name="products"><option>Roller banner</option><option>GrassHopper</option><option>Pop up display</option></select>';
cell3.innerHTML = '<select name="type"><option>Roller banner</option><option>GrassHopper</option><option>Pop up display</option></select>';
}
function removerow() {
document.getElementById("quote_form_table").deleteRow(-1);
}
</script>
As a novice at Javascript ,(and I use the term 'novice' very loosely as it is not a language I am familiar with to fix this myself), I tried setAttribute to no avail.
var tr = tr.setAttribute("name", "row[]", 0);
I found many sources on how to add classes or Id's but none for 'name'. So as a wild stab in the dark I tried a last attempt to see if I could work it out.
row.name = "row[]";
So now here I am.
The table rows are being made into arrays for PHP to process so that the information can be sent off in an email for price quotes and I can generate the information on the other end in the right order.
My question is How do you add a name="row[]" to the dynamically generated tr tag?
Is it even possible?
You want to add name to new rows, right?
var numRow = 0;
function addrow() {
var table = document.getElementById("quote_form_table");
var row = table.insertRow(-1);
row.setAttribute("name", "row"+numRow);
numRow++;
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = '<input type="text" name="quantity" >';
cell2.innerHTML = '<select name="products"><option>Roller banner</option><option>GrassHopper</option><option>Pop up display</option></select>';
cell3.innerHTML = '<select name="type"><option>Roller banner</option><option>GrassHopper</option><option>Pop up display</option></select>';
}
function removerow() {
document.getElementById("quote_form_table").deleteRow(-1);
numRow--;
}
But i think that you should use id or class instead of name for <tr> elements
I'm trying to create 2 dynamic html tables using Javascript with data from html inputs. I was able to create the first table I wanted but I've been unable to create 2 different tables using different inputs on the same page.
I tried changing the addRow() functions in the html and JS to have different names but this caused both tables to fail.
Any help would be appreciated. Here's the test code I've been using.
<!DOCTYPE html>
<body onload="load()">
<div id="myform">
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="name"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="number" id="age">
<input type="button" id="add" value="Add" onclick="addRow()"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br>
<table>
<tr>
<td>Height:</td>
<td><input type="text" id="height"></td>
</tr>
<tr>
<td>Width:</td>
<td><input type="text" id="width">
<input type="button" id="addDim" value="Add" onclick="addRow()"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</div>
<div id="mydata">
<table id="myTableData" border="1" cellpadding="2">
<tr>
<td> </td>
<td><b>Name</b></td>
<td><b>Age</b></td>
</tr>
</table>
<table id="myTableData2" border="1" cellpadding="2">
<tr>
<td> </td>
<td><b>Height</b></td>
<td><b>Width</b></td>
</tr>
</table>
</body>
</html>
function addRow() {
var myName = document.getElementById("name");
var age = document.getElementById("age");
var table = document.getElementById("myTableData");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML= '<input type="button" value = "Delete" onClick="deleteRow(this)">';
row.insertCell(1).innerHTML= myName.value;
row.insertCell(2).innerHTML= age.value;
var width = document.getElementById("width");
var height = document.getElementById("height");
var table2 = document.getElementById("myTableData2");
var rowCount2 = table2.rows.length;
var row2 = table2.insertRow(rowCount2);
row.insertCell(0).innerHTML = '<input type="button" value="Delete" onClick="deleteRow(this)">';
row.insertCell(1).innerHTML = width.value;
row.insertCell(2).innerHTML = height.value;
}
function deleteRow(obj) {
var index = obj.parentNode.parentNode.rowIndex;
var table = document.getElementById("myTableData");
table.deleteRow(index);
}
function load() {
console.log("Page load finished");
}
Looks like in the bottom section, you're not using the row2 variable you've defined.
Should be:
var rowCount2 = table2.rows.length;
var row2 = table2.insertRow(rowCount2);
row2.insertCell(0).innerHTML = '<input type="button" value="Delete" onClick="deleteRow(this)">';
row2.insertCell(1).innerHTML = width.value;
row2.insertCell(2).innerHTML = height.value;
here how i did it. you can use the same function for any amount of table, if pass the parameter in the function.
http://jsfiddle.net/8t5aqomk/2/
function myFunction(thisTable) {
var firstName = document.getElementById('firstName').value;
var lastName = document.getElementById('lastName').value;
var age = document.getElementById('age').value;
var info = [firstName,lastName,age]
var table = document.getElementById(thisTable);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cellCount = table.rows[0].cells.length;
for(var i=0; i<cellCount; i++) {
row.insertCell(i).innerHTML=info[i];
}
}