Adding a new item to a checklist - javascript

I am trying to create a checklist that allows users to add their own items.
This is part of my HTML:
<tr>
<td>
<input name="checkbox65" id="checkbox65" type="checkbox" />
</td>
<td>
<label for="checkbox65">Get directions for where you are going</label>
</td>
</tr>
<h3>
My items
</h3>
<fieldset data-role="controlgroup">
<label for="textinput4">Add new item</label>
</td>
<input name="new_item" id="textinput4" placeholder="" value="" type="text"
/>
</fieldset>
<input type="submit" id="add" value="Add" />
And this is my script:
<script>
$('#add').on('click', function (e) {
var $this = $(this);
var $newRow = $this.closest('table').find('tr:first').clone();
$newRow.find(':input').val('');
$newRow.insertAfter($this.parent());
});
</script>
But I just do not get any response..

Is this what you need ?
HTML
<h3>My items</h3>
<table id="myTable">
<tr>
<td>
<label for="checkbox65">
<input name="checkbox65" class="checkbox65" type="checkbox" />
Get directions for where you are going
</label>
</td>
</tr>
<tr><td>
<fieldset data-role="controlgroup">
<label for="textinput4">
Add new item
<input name="new_item" id="textinput4" placeholder="" value="" type="text" />
</label>
</fieldset>
<button id="add">Add</button>
</td></tr>
</table>
Notice I've changed id="checkbox65" to class="checkbox65" because you can't use same id twice. Also changed input type=submit to <button> for adding new elements.
JS
$('#add').on('click', function (e) {
var $this = $(this);
var $firstRow=$this.closest('table').find('tr:first');
var $newRow = $firstRow.clone();
$newRow.find(':input').prop('checked', false);
$newRow.insertAfter($firstRow);
});
DEMO.
Update: Or maybe this one or this one.

Related

Enable Edit button if new record exists

I created a modal where I can insert new client record and edit the same record. But in relation to the edit button, I wanted it to be active only when there was a new record in the database and until the end of the day it was registered.
<a type="button" name="editar" id="'.$row["IdCliente"].'" data-toggle="modal" href="#add_data_Modal" class="btn btn-primary edit_data">Update</a>
I'm trying this way:
if(isset($_POST["employee_id3"]))
{
$output = '';
$query = "SELECT * FROM centrodb.PsicUtentes LEFT OUTER JOIN centrodb.PsicUtentesConsulta ON centrodb.PsicUtentesConsulta.CodigoUtente2 = centrodb.PsicUtentes.CodigoUtente WHERE centrodb.PsicUtentes.Id = '".$_POST["employee_id3"]."' ORDER BY IdConsulta DESC ";
$result = mysqli_query($conn, $query);
$output;
while($row = mysqli_fetch_array($result))
{
$dataAtual = DATE('Y-m-d');
$disabled = "";
if(DATE($row['Data2'])!=$dataAtual){
$disabled = "disabled";
}
$output .= '
<h4 class="modal-title">Histórico de Consultas</h4>
<div>
<button type="button" class="exibir botao" href="#" aria-hidden="true">+</button>
<button type="button" href="#" class="ocultar botao" aria-hidden="true">-</button>
</div>
<div class="conteudo">
<form method="post" id="insert_form6">
<div style="float:right">
<a type="button" name="edit3" id="'.$row["Id"].','.$row["IdConsulta"].'" data-toggle="modal" href="#add_data_Modal3" class="btn btn-primary edit_data3" "$disabled">Editar</a>
</div>
<br/>
<br/>
<br/>
<fieldset class="grupo">
<table class="campo" cellspacing="10">
<tr>
<td>
<input type="Hidden" id="IdConsulta1" name="IdConsulta" class="form-control" value="'.$row["IdConsulta"].'" style="width:150px;" readonly="true" />
</td>
<td>
<label>Data Consulta</label>
<input type="text" id="Data23" name="Data2" class="form-control" value="'.$row["Data2"].'" style="width:150px;" readonly="true" />
</td>
<td>
<label>Código Utente</label>
<input type="number" id="CodigoUtente5" name="CodigoUtente" value="'.$row["CodigoUtente"].'" class="form-control" style="width:100px;" readonly="true"/>
</td>
<td>
<label>Nome Utente</label>
<input type="text" id="Nome5" name="Nome" value="'.$row["Nome"].'" class="form-control" style="width:400px;" readonly="true"/>
</td>
<td>
<label>Data Nascimento</label>
<input type="date" id="DataNasc5" name="DataNasc" value="'.$row["DataNasc"].'" class="form-control" style="width:150px;" readonly="true"/>
</td>
</tr>
</table>
</fieldset>
<fieldset class="grupo">
<table class="campo" cellspacing="10">
<tr>
<td>
<label>Data Admissao</label>
<input type="date" id="DataAdmissao5" name="DataAdmissao" value="'.$row["DataAdmissao"].'" class="form-control" style="width:150px;" readonly="true"/>
</td>
<td>
<label>Valência</label>
<input type="text" id="ValenciasDescricao5" name="ValenciasDescricao" value="'.$row["ValenciasDescricao"].'" class="form-control" style="width:200px;" readonly="true"/>
</td>
</tr>
</table>
</fieldset>
<fieldset class="grupo">
<table class="campo" cellspacing="10">
<tr>
<td>
<label>Observação</label>
</p><textarea rows="6" cols="130" readonly="true">'.$row["Descricao"].'</textarea>
</td>
</tr>
</table>
</fieldset>
<fieldset class="grupo">
<table class="campo" cellspacing="10">
<tr>
<td>
<label>O Psicologo/a</label>
<input type="text" id="Colaborador2" name="Colaborador2" class="form-control" style="width:150px;" value="'.$row["Colaborador2"].'" readonly="true"/>
</td>
</tr>
</table>
</fieldset>
</form>
</div>
';
}
$output;
echo $output;
}
<script>
$(document).ready(function(){
$(document).on('click', '.edit_data3', function(){
var employee_id3 = $(this).attr("Id");
$.ajax({
url:"./fetch2",
method:"POST",
data:{employee_id3:employee_id3},
dataType:"json",
success:function(data){
$('#IdConsulta').val(data.IdConsulta);
$('#Data22').val(data.Data2);
$('#CodigoUtente6').val(data.CodigoUtente2);
$('#Descricao1').val(data.Descricao);
$('#Colaborador2').val(data.Colaborador2);
$('#employee_id3').val(data.Id);
$('#insert3').val("Gravar");
$('#add_data_Modal3').modal('show');
}
});
});
$('#insert_form7').on("submit", function(event){
event.preventDefault();
if($('#CodigoUtente6').val() == "")
{
alert("Código Utente é necessário");
}
else if($('#Descricao1').val() == "")
{
alert("Observação é necessária");
}
else
{
$.ajax({
url:".conexao9",
method:"POST",
data:$('#insert_form7').serialize()
,
beforeSend:function(){
$('#insert3').val("Inserting");
},
success:function(data){
$('#insert_form7')[0].reset();
$('#add_data_Modal3').modal('hide');
$('#employee_table').html(data);
location.reload("add_data_Modal3");
}
});
}
});
});
</script>
I created a variable with the current date that compares with the date of the record. I created an empty disabled variable. There I put this disabled variable in the link. If the date is different than the current date you put the disabled variable filled.The problem is that the button stays in the same asset.
Firstly your code is vulnerable to sql injection attacks and you should use prepared statements.
Then you have some syntax errors on your code.
The lines $output; are useless, delete them.
Change "$disabled" to '.$disabled'.
and put ?> before your <script> tag.

submit input text into table as <td>

Ive been looking online for super long and I haven't been able to find an answer to something that seems super common.
Basically what I want to do is submit input text and maybe also if a radio button or something like that is checked to popup as text in a table as here are some photos of the inputs and the table.
I want to be able to press the "+" or "add" button and have all the text from the inputs added into the table and if the radio is checked (checked= true(something like that)) for it all to show up in the table when I press the add button.
Here is my HTML for the example photo:
<div class="bg">
<button id="droplist-btn" class="droplist-btn">Add By Droplist</button>
<form>
<input type="text" id="item-code-input" class="item-code-input" placeholder=""></input>
<button type="submit" id="add-item" class="add-item">+</button>
<input type="text" id="color-input" placeholder="color"></input>
<select type="text" id="sizes-item-code" class="sizes-item-code">
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
<option value="XLarge">XLarge</option>
<option value="One Size">One Size</option>
</select>
</label>
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
<label for="switch">Any Color</label>
</form>
<table id="items-table">
<!--<caption>Task</caption>-->
<tr>
<th>Item</th>
<th>Size</th>
<th>Options</th>
</tr>
<tr>
</tr>
</table>
<label id="item-code-label">Item Code</label>
<!-- idk what happened here -->
<label class="radio">.
<input type="radio" checked="checked" name="radio">
<span class="check-item"></span>
</label>
<label id="keyword-label">Keyword</label>
<label class="radio">.
<input type="radio" name="radio">
<span class="check-keyword"></span>
</label>
</div>
<script src="jquery.js"></script>
<script src="items.js"></script>
</body>
</html>
I have tried to add both of the answers below to my other script and none of them are working here is the html for my differnt table:
<table id="proxy-list">
<tr>
<!--<th>Name</th> -->
<th colspan="3">Proxies</th>
</tr>
<tr>
<td>Proxy1:</td>
<td>174.32.116.214:87</td>
<td>
<button class="remove-btn"><div class="thex">x</div></button>
</td>
</tr>
</table>
<input type="text" id="proxy-add-name" placeholder="Name"></input>
<input type="text" id="proxy-add-proxy" placeholder="Proxy"></input>
<button id="proxy-add" onclick="addProxy()">Add</button>
Any help is appreciated thank you.
(I Know my html code is very messy im very knew to html and am still learning)
(jQuery is preferred but as I said anything helps)
Just update the start of addRow
function addRow() {
var item_text = 1; // $('#item-code-input').val() ???
var size_text = 2;
var options_text = 3;
$('#main-table').append('<tr>'
+'<td>'+item_text+'</td>'
+'<td>'+size_text+'</td>'
+'<td>'+options_text+'</td>'
+'</tr>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="main-table">
<tr>
<th>Item</th><th>Size</th><th>Options</th>
</tr>
</table>
<button onclick="addRow()">Add Row</button>
This is using your code - BTW I added id="switch" for the checkbox input (to link to for="switch") For options it only takes into account "Any Colour" though. The "+" button makes it add rows. I used "return false" to stop the form from submitting when the + button is clicked.
function addRow() {
var item_text = $('#item-code-input').val();
var size_text = $('#sizes-item-code').val();
var options_text = $('#switch').is(':checked') ? 'Any Color' : '';
$('#items-table').append('<tr>'
+'<td>'+item_text+'</td>'
+'<td>'+size_text+'</td>'
+'<td>'+options_text+'</td>'
+'</tr>');
}
$(function(){
$('#add-item').click(function(){
addRow();
return false;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bg">
<button id="droplist-btn" class="droplist-btn">Add By Droplist</button>
<form>
<input type="text" id="item-code-input" class="item-code-input" placeholder=""></input>
<button type="submit" id="add-item" class="add-item">+</button>
<input type="text" id="color-input" placeholder="color"></input>
<select type="text" id="sizes-item-code" class="sizes-item-code">
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
<option value="XLarge">XLarge</option>
<option value="One Size">One Size</option>
</select>
</label>
<label class="switch">
<input type="checkbox" id="switch">
<span class="slider"></span>
</label>
<label for="switch">Any Color</label>
</form>
<table id="items-table">
<!--<caption>Task</caption>-->
<tr>
<th>Item</th>
<th>Size</th>
<th>Options</th>
</tr>
<tr>
</tr>
</table>
<label id="item-code-label">Item Code</label>
<!-- idk what happened here -->
<label class="radio">.
<input type="radio" checked="checked" name="radio">
<span class="check-item"></span>
</label>
<label id="keyword-label">Keyword</label>
<label class="radio">.
<input type="radio" name="radio">
<span class="check-keyword"></span>
</label>
</div>
<script src="jquery.js"></script>
<script src="items.js"></script>
</body>
</html>
Try this out. Not efficient but it covers what you want to do
function add() {
let table = document.getElementById("theTable")
let fd = new FormData(myForm)
let output = ""
for(let data of fd.entries()) {
output += "<td>"+data[1]+"</td>"
}
output = "<tr>"+output+"</tr>"
table.innerHTML += output
}
<form id=myForm>
<input name="name" value="John" />
<input name="age" value="32" />
<input name="lang" value="en" />
<input type=button onclick="add()" value="+"/>
</form>
<table width="100%" border=1 id="theTable">
</table>

how to check the default value of a radiobutton tag and display its related contents?

below is the code to check a default value for the radiobutton tag by giving checked="checked", but i am not able to display the related form by default, it can be displayed only when i click on it, how to display anyone of these forms by default ?
HTML:
<form>
<label><input value="1" type="radio" name="formselector" checked="checked" onclick="displayForm(this)">Old Definitions</label>
<label><input value="2" type="radio" name="formselector" onclick="displayForm(this)">New Definition</label>
</form>
<panel method="post" style="visibility:hidden" id="form1" name="form1">
<table style="width:100%;">
<tr>
<th ><span class="dropt">ID Reserve Tracks</th>
<td>
<input id="idtracks" autocomplete="off" name="idtracks" type="text" maxlength="50" >
</td>
</tr>
</table>
</panel>
<panel style="visibility:hidden" id="form2">
<table style="width:100%;">
<th ><span class="dropt">MD Reserve Tracks</th>
<td>
<input id="mdtracks" autocomplete="off" name="mdtracks" type="text" maxlength="50" >
</td>
</table>
</panel>
JavaScript:
function displayForm(c){
if(c.value == "1"){
document.getElementById("form1").style.visibility='visible';
document.getElementById("form2").style.visibility='collapse';
}
else if(c.value =="2"){
document.getElementById("form1").style.visibility='collapse';
document.getElementById("form2").style.visibility='visible';
}
else{
}
}
jsfiddle: http://jsfiddle.net/uLkHk/
Your Html is not well formed, you should improve that.
To make it work as you want, just do not set the option checked in HTML. Then select it in JavaScript, calling the Click method.
See this, I modified a little your HTML, and it works.
<html>
<body>
<form>
<label>
<input value="1" type="radio" id="radioOld" name="formselector" onclick="displayForm(this)" />Old
Definitions</label>
<label>
<input value="2" type="radio" id="radioNew" name="formselector" onclick="displayForm(this)" />New
Definition</label>
</form>
<form method="post" style="visibility: hidden" id="form1" name="form1">
<table style="width: 100%;">
<tr>
<td>
<span class="dropt">ID Reserve Tracks</span>
</td>
<td>
<input id="idtracks" autocomplete="off" name="idtracks" type="text" maxlength="50" />
</td>
</tr>
</table>
</form>
<form style="visibility: hidden" id="form2">
<table style="width: 100%;">
<tr>
<td>
<span class="dropt">MD Reserve Tracks</span>
</td>
<td>
<input id="mdtracks" autocomplete="off" name="mdtracks" type="text" maxlength="50" />
</td>
</tr>
</table>
</form>
<script>
function displayForm(c) {
if (c.value == "1") {
document.getElementById("form1").style.visibility = 'visible';
document.getElementById("form2").style.visibility = 'collapse';
}
else if (c.value == "2") {
document.getElementById("form1").style.visibility = 'collapse';
document.getElementById("form2").style.visibility = 'visible';
}
else {
}
}
</script>
<script>
document.getElementById('radioOld').click();
//document.getElementById('radioOld').checked = true;
//var theDefaultOption = document.getElementById('formselector');
//displayForm(theDefaultOption);
</script>
</body>
</html>
Or you can simply set the default panel's visibility to visible in the HTML as Barmar said.

Assingning unique id and names?

I have a html table with one prototype row. Prototype row has few tds and each td has input element. I am trying to clone the row and assign unique ids and names. But in Fire fox new ids and names are assigned properly. But in IE unique ids and names are not assigned. This logic works fine in FF. But in IE7 it does not work.
html Table:
<table class="myTable">
<tr class="prototype">
<td>
<label for="myValue1">MY Value ONE:</label><br>
<input class="required email" id="myValue1" type="text" value="" name="myValue1">
</td>
<td>
<label for="myValue2">MY Value TWO:</label><br>
<input class="required email" id="myValue2" type="text" value="" name="myValue2">
</td>
<td>
<label for="myValue3">MY Value THREE:</label><br>
<input class="required email" id="myValue3" type="text" value="" name="myValue3">
</td>
<td>
<label for="myValue4">MY Value FOUR:</label><br>
<input class="required email" id="myValue4" type="text" value="" name="myValue4">
</td>
</tr>
</table>
JQuery code:
$("#new").click(function(e) {
e.preventDefault();
var d = new Date();
var counter = d.getTime();
var master = $("table.myTable");
var prot = master.find("tr.prototype").clone();
prot.removeClass('prototype');
prot.addClass("contact");
prot.find("#myValue1").attr('id',"myValue1"+counter);
prot.find("#myValue2").attr('id',"myValue2"+counter);
prot.find("#myValue3").attr('id',"myValue3"+counter);
prot.find("#myValue4").attr('id',"myValue4"+counter);
prot.find("#myValue1"+counter).attr('name',"myValue1"+counter);
prot.find("#myValue2"+counter).attr('name',"myValue2"+counter);
prot.find("#myValue3"+counter).attr('name',"myValue3"+counter);
prot.find("#myValue4"+counter).attr('name',"myValue4"+counter);
jQuery('table.myTable tr:last').before(prot);
});
But with the above code unique ids and names are not assigned.am i doing anything wrong here?
Thanks!
You don't needs IDs:
<label>MY Value ONE:<br /><input class="required email" type="text" name="myValue1[]" /></label>
Now you can clone this label as many times as needed, and it'll work.
On the server side, you can then access (for example in PHP) $_POST['myValue1'] as an array.
Sorry to tell you, but everything seems fine with what you did. I tried the example you gave on IE and it seems to work.
If you inspect it with Developer Tools you still see the old id? what version of IE?
here is the code I tried:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(function() {
$("#new").click(function(e) {
e.preventDefault();
var d = new Date();
var counter = d.getTime();
var master = $("table.myTable");
var prot = master.find("tr.prototype").clone();
prot.removeClass('prototype');
prot.addClass("contact");
//$(prot.find("input")[0])
prot.find("#myValue1").attr('id',"myValue1"+counter);
prot.find("#myValue2").attr('id',"myValue2"+counter);
prot.find("#myValue3").attr('id',"myValue3"+counter);
prot.find("#myValue4").attr('id',"myValue4"+counter);
prot.find("#myValue1"+counter).attr('name',"myValue1"+counter);
prot.find("#myValue2"+counter).attr('name',"myValue2"+counter);
prot.find("#myValue3"+counter).attr('name',"myValue3"+counter);
prot.find("#myValue4"+counter).attr('name',"myValue4"+counter);
jQuery('table.myTable tr:last').before(prot);
counter++;
});
});
</script>
</head>
<body>
<table class="myTable">
<tr class="prototype">
<td>
<label for="myValue1">MY Value ONE:</label><br>
<input class="required email" id="myValue1" type="text" value="" name="myValue1">
</td>
<td>
<label for="myValue2">MY Value TWO:</label><br>
<input class="required email" id="myValue2" type="text" value="" name="myValue2">
</td>
<td>
<label for="myValue3">MY Value THREE:</label><br>
<input class="required email" id="myValue3" type="text" value="" name="myValue3">
</td>
<td>
<label for="myValue4">MY Value FOUR:</label><br>
<input class="required email" id="myValue4" type="text" value="" name="myValue4">
</td>
</tr>
</table>
<input type="button" id="new" />
</body>
</html>

Does JQuery Mobile interfere with Javascript or is it an issue with formatting?

I know the code works because it was tested in JSfiddle, but it's something about putting it into this HTML that then causes an error, unless JQuery Mobile it interfering?
So I have stripped away the CSS in order to test, but no matter what I take away it just causes error when adding a new item to the list.
Here is the working version:
DEMO
<title>Checklist</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.1.0/jquery.mobile-1.1.0.min.js">
</script>
<script>
$('#add').on('click', function (e) {
var $this = $(this);
var $firstRow=$this.closest('table').find('tr:first');
var $newRow = $firstRow.clone();
var input = $newRow.find(':input').remove();
input.prop('checked', false);
$newRow.empty().append(input).append(' '+$('#textinput4').val());
$newRow.insertAfter($firstRow);
});​
</script>
</head>
<body>
<div data-role="page" id="check">
<div data-theme="a" data-role="header">
<h3>
Checklist
</h3>
<a data-role="button" data-transition="fade" href="#about" class="ui-btn-left">
Back
</a>
</div>
<div data-role="content" style="padding: 15px">
<div data-role="collapsible-set">
<form name="emailform" enctype="multipart/form-data" action="form-to-email.php" method="get">
<div data-role="fieldcontain">
<label for='name'>Festival name: </label><br>
<input type="text" name="name">
<h3>
Before you go
</h3>
<fieldset data-role="controlgroup" data-type="vertical">
<legend>
</legend>
<input name="checkbox62" id="checkbox62" type="checkbox" />
<label for="checkbox62">
Charge all batteries. Phones, cameras etc
</label>
<input name="checkbox63" id="checkbox63" type="checkbox" />
<label for="checkbox63">
Save your friends numbers
</label>
<input name="checkbox64" id="checkbox64" type="checkbox" />
<label for="checkbox64">
Check out festival site rules!
</label>
<tr><td><input name="checkbox65" id="checkbox65" type="checkbox" /></td>
<td><label for="checkbox65">Get directions for where you are going</label></td></tr>
</fieldset>
<h3>My items</h3>
<table id="myTable">
<tr>
<td>
<label for="checkbox65">
<input name="checkbox65" class="checkbox65" type="checkbox" />
Get directions for where you are going
</label>
</td>
</tr>
<tr>
<td>
<fieldset data-role="controlgroup">
<label for="textinput4">
Add new item
<input name="new_item" id="textinput4" placeholder="" value="" type="text" />
</label>
</fieldset>
<button id="add">Add</button>
</td>
</tr>
</table>​
</form>
This is simply a case of your event handler being bound to early. When you are trying to bind your click event to the add button the add button hasn't been loaded into the DOM yet. One thing which you can do is simply move your script to the end of your body.
A better solution would be to use event delegation, for example
$(document).on('click', '#add', function(e) {
var $this = $(this);
var $firstRow = $this.closest('table').find('tr:first');
var $newRow = $firstRow.clone();
var input = $newRow.find(':input').remove();
input.prop('checked', false);
$newRow.empty().append(input).append(' ' + $('#textinput4').val());
$newRow.insertAfter($firstRow);
});
(You might still want to move your script to the end of your page for performance reasons so that it doesn't block the loading of your page).

Categories