I have a table that looks like this
<table id="tableScanItems" class="table">
<th>Scan Item</th>
<th>Inactive</th>
<tr>
<td><input type='text' id='input1' class='form-control'/></td>
<td><input type='checkbox' id='checkbox1' class='form-control'/></td>
</tr>
<tr>
<td><input type='text' id='input2' class='form-control'/></td>
<td><input type='checkbox' id='checkbox2' class='form-control'/></td>
</tr>
<tr>
<td><input type='text' id='input3' class='form-control'/></td>
<td><input type='checkbox' id='checkbox3' class='form-control'/></td>
</tr>
</table>
On a button click I have this jQuery that runs.
$("#tableScanItems tr").each(function (){
$(this).closest('tr').find("input").each(function() {
...get the values for each row and do stuff
});
});
I need to loop the html table to get the values of the input type=text and the input type=checkbox
The JS does loop properly and gets the input type=text values but not the input type=checkbox values. Do I need to specify a different parameter in the .each to see the value of checkbox?
Ryan
EDIT
If I do this I cannot see the values of the user inputs.
$(this).closest('tr').find("input").each(function() {
alert(this.value)
});
You can loop like below. You need to treat checkboxes differently to get their values:
$("#tableScanItems tr").each(function (){
$(this).closest('tr').find("input").each(function() {
var elem = $(this);
if (elem.is(':checkbox')) {
console.log( !!elem.attr("checked"));
} else {
console.log(elem.val());
}
});
});
Working fiddle here: https://jsfiddle.net/5yt9hon1/1/
Check some checkboxes and hit submit. You will see the values of the checkboxes in a div.
$(document).ready(function () {
$('#btnSubmit').click(function () {
var result = $('input[type="checkbox"]:checked')
if (result.length > 0) {
var resultString = result.length + " checked <br/><br/>"
result.each(function () {
resultString+= $(this).val() + "<br/>";
});
$('#divCheckboxValues').html(resultString);
}
else {
$('#divCheckboxValues').html(" No checkbox checked");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table id="tableScanItems" class="table">
<th>Scan Item</th>
<th>Inactive</th>
<tr>
<td><input type='text' id='input1' class='form-control'/></td>
<td><input type='checkbox' id='checkbox1' class='form-control'/></td>
</tr>
<tr>
<td><input type='text' id='input2' class='form-control'/></td>
<td><input type='checkbox' id='checkbox2' class='form-control'/></td>
</tr>
<tr>
<td><input type='text' id='input3' class='form-control'/></td>
<td><input type='checkbox' id='checkbox3' class='form-control'/></td>
</tr>
</table>
<input id="btnSubmit" type="submit" value="submit" />
<br />
<div id="divCheckboxValues">
</div>
Related
I'm working on dynamic 3 column table that has 3 input text type : Product, quantity and date
This works good but i want to change type format:
Product column => Dropdown type
Quantity column => number type
Date column => Date type (dd-mm-yyyy)
How i do change it?
Thank you
Original source: link
Code online: https://jsfiddle.net/bvotcode/r61ptxe9/7/
HTML
<body>
<div id="wrapper">
<table align='center' cellspacing=2 cellpadding=5 id="data_table" border=1>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Date</th>
</tr>
<tr id="row1">
<td id="product_row1">Car</td>
<td id="quantity_row1">10</td>
<td id="date_row1">20/12/2021</td>
<td>
<input type="button" id="edit_button1" value="Edit" class="edit" onclick="edit_row('1')">
<input type="button" id="save_button1" value="Save" class="save" onclick="save_row('1')">
<input type="button" value="Delete" class="delete" onclick="delete_row('1')">
</td>
</tr>
<tr id="row2">
<td id="product_row2">Book</td>
<td id="quantity_row2">22</td>
<td id="date_row2">26</td>
<td>
<input type="button" id="edit_button2" value="Edit" class="edit" onclick="edit_row('2')">
<input type="button" id="save_button2" value="Save" class="save" onclick="save_row('2')">
<input type="button" value="Delete" class="delete" onclick="delete_row('2')">
</td>
</tr>
<tr id="row3">
<td id="product_row3">Laptop</td>
<td id="quantity_row3">44</td>
<td id="date_row3">19/01/2022</td>
<td>
<input type="button" id="edit_button3" value="Edit" class="edit" onclick="edit_row('3')">
<input type="button" id="save_button3" value="Save" class="save" onclick="save_row('3')">
<input type="button" value="Delete" class="delete" onclick="delete_row('3')">
</td>
</tr>
<tr>
<td><input type="text" id="new_product"></td>
<td><input type="text" id="new_quantity"></td>
<td><input type="text" id="new_date"></td>
<td><input type="button" class="add" onclick="add_row();" value="Add Row"></td>
</tr>
</table>
</div>
</body>
</html>
</body>
CSS
.pt-3-half { padding-top: 1.4rem; }
Javascript
function edit_row(no)
{
document.getElementById("edit_button"+no).style.display="none";
document.getElementById("save_button"+no).style.display="block";
var product=document.getElementById("product_row"+no);
var quantity=document.getElementById("quantity_row"+no);
var date=document.getElementById("date_row"+no);
var product_data=product.innerHTML;
var quantity_data=quantity.innerHTML;
var date_data=date.innerHTML;
product.innerHTML="<input type='text' id='product_text"+no+"' value='"+product_data+"'>";
quantity.innerHTML="<input type='text' id='quantity_text"+no+"' value='"+quantity_data+"'>";
date.innerHTML="<input type='text' id='date_text"+no+"' value='"+date_data+"'>";
}
function save_row(no)
{
var product_val=document.getElementById("product_text"+no).value;
var quantity_val=document.getElementById("quantity_text"+no).value;
var date_val=document.getElementById("date_text"+no).value;
document.getElementById("product_row"+no).innerHTML=product_val;
document.getElementById("quantity_row"+no).innerHTML=quantity_val;
document.getElementById("date_row"+no).innerHTML=date_val;
document.getElementById("edit_button"+no).style.display="block";
document.getElementById("save_button"+no).style.display="none";
}
function delete_row(no)
{
document.getElementById("row"+no+"").outerHTML="";
}
function add_row()
{
var new_product=document.getElementById("new_product").value;
var new_quantity=document.getElementById("new_quantity").value;
var new_date=document.getElementById("new_date").value;
var table=document.getElementById("data_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='product_row"+table_len+"'>"+new_product+"</td><td id='quantity_row"+table_len+"'>"+new_quantity+"</td><td id='date_row"+table_len+"'>"+new_date+"</td><td><input type='button' id='edit_button"+table_len+"' value='Edit' class='edit' onclick='edit_row("+table_len+")'> <input type='button' id='save_button"+table_len+"' value='Save' class='save' onclick='save_row("+table_len+")'> <input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'></td></tr>";
document.getElementById("new_product").value="";
document.getElementById("new_quantity").value="";
document.getElementById("new_date").value="";
}
You're using the type="text" for all of the <input> tags. You should consider changing the type of the inputs in your edit_row function.
For numbers, the type of input should be number;
For dates, date;
And finally, to render a dropdown box, the best solution is to use a <select> tag instead of an <input>. Don't forget to write all the options of your dropdown inside of <option> tags (which should be all inside of one <select> tag); Here you can find a simple explanation on how to use the <select> tag.
my jquery code doesn't work on dynamically appended elements. It is working fine for the elements that already present there but When I embed a new row using the jQuery append function, then the jquery code no longer works.
Here is the jquery code that embeds a new row
<script>
$(document).ready(function() {
$('#add_row').click(function(){
$('#sale_table').append('<tr><td><select class="form-control" name="item_name[]"><option>Select an Item</option><?php while($item_result = mysqli_fetch_assoc($select_item_query)) { ?><option><?php echo $item_result['item_name']; ?></option><?php } ?></select></td><td><input type="text" class="form-control" name="item_quantity[]" id="sale_item_quantity" placeholder="0.00"></td><td><input type="text" class="form-control" name="item_price[]" id="sale_item_price" placeholder="0.00"></td><td><input type="text" class="form-control" name="item_discount[]" id="sale_item_discount" placeholder="%"></td><td><input type="text" class="form-control" name="item_total_amount[]" id="sale_item_total_amount" placeholder="0.00"></td></tr>');
});
});
</script>
and this is the jquery code that I'm trying to run on appended row. This code doesn't work on appended rows
<script>
$(document).ready(function() {
$('#sale_item_price, #sale_item_quantity').on('keyup', function(){
var sum = 0;
$('.sale-entry > tbody > tr').each(function() {
var qty = $(this).find('#sale_item_quantity').val();
var price = $(this).find('#sale_item_price').val();
var discount = $(this).find('#sale_item_discount').val();
var amount = (qty * price);
if(discount){
amount = amount - discount;
}
$(this).find('#sale_item_total_amount').val(amount);
sum += amount;
});
$('#sale_subtotal_amount').text(sum);
$('#sale_total_amount').text(sum);
$("#sale_item_discount").on('keyup', function() {
var discount = $("#sale_item_discount").val();
$("#sale_item_total_amount").val(sum - discount);
$("#sale_subtotal_amount").text(sum - discount);
$("#sale_total_amount").text(sum - discount);
});
});
});
</script>
and here is the HTML code
<table class="table sale-entry" id="sale_table">
<thead class="thead-light">
<tr>
<th scope="col">Item Name</th>
<th scope="col">Quantity</th>
<th scope="col">Rate</th>
<th scope="col">Discount</th>
<th scope="col">Total Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td><select class="form-control" name="item_name[]">
<option>Select an Item</option>
<?php while($item_result = mysqli_fetch_assoc($select_item_query)) { ?>
<option><?php echo $item_result['item_name']; ?></option>
<?php } ?>
</select>
</td>
<td><input type="text" class="form-control" name="item_quantity[]" id="sale_item_quantity" placeholder="0.00"></td>
<td><input type="text" class="form-control" name="item_price[]" id="sale_item_price" placeholder="0.00"></td>
<td><input type="text" class="form-control" name="item_discount[]" id="sale_item_discount" placeholder="%"></td>
<td><input type="text" class="form-control" name="item_total_amount[]" id="sale_item_total_amount" placeholder="0.00"></td>
</tr>
</tbody>
</table>
What could be the issue?
if $('#sale_item_price, #sale_item_quantity').on('keyup'... not working then $(document).on('keyup', '#sale_item_price, #sale_item_quantity',... works.
Here, $('#sale_item_price, #sale_item_quantity') is unaware of DOM changed.
Have a look in example:-
$(document).ready(function() {
$(document).on('keyup', '#sale_item_price, #sale_item_quantity, #sale_item_discount', function() {
var sum = 0;
$('.sale-entry > tbody > tr').each(function() {
var qty = $(this).find('#sale_item_quantity').val();
var price = $(this).find('#sale_item_price').val();
var discount = $(this).find('#sale_item_discount').val();
var amount = (qty * price);
if (discount) {
amount = amount - discount;
}
$(this).find('#sale_item_total_amount').val(amount);
sum += amount;
});
$('#sale_subtotal_amount').text(sum);
$('#sale_total_amount').text(sum);
});
});
$(document).ready(function() {
$('#add_row').click(function() {
$('#sale_table').append('<tr><td><select class="form-control" name="item_name[]"><option>Select an Item</option></select></td><td><input type="text" class="form-control" name="item_quantity[]" id="sale_item_quantity" placeholder="0.00"></td><td><input type="text" class="form-control" name="item_price[]" id="sale_item_price" placeholder="0.00"></td><td><input type="text" class="form-control" name="item_discount[]" id="sale_item_discount" placeholder="%"></td><td><input type="text" class="form-control" name="item_total_amount[]" id="sale_item_total_amount" placeholder="0.00"></td></tr>');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table sale-entry" id="sale_table">
<thead class="thead-light">
<tr>
<th scope="col">Item Name</th>
<th scope="col">Quantity</th>
<th scope="col">Rate</th>
<th scope="col">Discount</th>
<th scope="col">Total Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-control" name="item_name[]">
<option>Select an Item</option>
</select>
</td>
<td><input type="text" class="form-control" name="item_quantity[]" id="sale_item_quantity" placeholder="0.00"></td>
<td><input type="text" class="form-control" name="item_price[]" id="sale_item_price" placeholder="0.00"></td>
<td><input type="text" class="form-control" name="item_discount[]" id="sale_item_discount" placeholder="%"></td>
<td><input type="text" class="form-control" name="item_total_amount[]" id="sale_item_total_amount" placeholder="0.00"></td>
</tr>
</tbody>
</table>
<button id="add_row" type="button">Add row</button>
<div>sale_subtotal_amount: <span id="sale_subtotal_amount"></span></div>
<div>sale_total_amount: <span id="sale_total_amount"></span></div>
I am trying to create a simple login page for a server:
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form name="loginForm" method="post" action="login.php">
<table>
<tr>
<td colspan=2><center><font size=4><b>HTML Login Page</b></font>
</center></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" size=25 name="userid"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="Password" size=25 name="pwd"></td>
</tr>
<tr>
<td ><input type="Reset"></td>
<td><input type="submit" onclick="return check(this.form); load();"
value="Login">
</td>
</tr>
</table>
</form>
<script language="javascript">
function check(form)
{
if(form.userid.value == "n" && form.pwd.value == "n")
{
return true;
}
else
{
alert("Error Password or Username")
return false;
}
</script>
</body>
</html>
The HTML above is how i would like the page to look, however i want to add another function that when the login button is pressed i would like the page to take me to a different page like google for example.
Is there anyway I can do this?
Through Html you can do something like:
<form action="http://google.com">
<input type="submit" value="Go to Google">
</form>
Following code is used to highlight a table record when a checkbox is clicked. When the first checkbox clicked table record highlighted in red color, and when second checkbox is clicked table record should highlighted in yellow color and when the third checkbox is clicked table record should be highlighted in green color.Although I have used 3 colors at style none of them highlight the record when I click them
<style>
.cb3 {background-color:yellow;}
.cb2 {background-color:green;}
.cb1 {background-color:red;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
var checked = [];
$(document).ready(function() {
if (localStorage.getItem("checked") == null)
localStorage.setItem("checked", checked);
$("#Table input").click(function() {
if ($(this).is(":checked")) {
$(this).parent().parent().addClass("highlight");
checked.push($(this).attr("id"));
} else {
$(this).parent().parent().removeClass("highlight");
checked.remove($(this).attr("id"));
}
localStorage.setItem("checked", JSON.stringify(checked));
});
var saved = JSON.parse(localStorage.getItem("checked"));
for (var i = 0;i < saved.length; i++) {
var itemAtIndex = $("#" + saved[i] + "");
itemAtIndex.click();
itemAtIndex.parent().parent().addClass("highlight");
}
});
</script>
<body>
<div class="col-lg-10">
<table id="Table" border="1"><tr>
<td><input type="checkbox" name="cb1" id="cb1" value="y" /></td>
<td>Click me</td>
</tr><tr>
<td><input type="checkbox" name="cb2" id="cb2" value="y" /></td>
<td>Click me</td>
</tr><tr>
<td><input type="checkbox" name="cb3" id="cb3" value="y" /></td>
<td>Click me</td>
</tr></
<table>
</div>
Your JS was correct. In your HTML, you just need to move each id to the <tr> row and add a class to each table record.
<tr id="cb1">
<td><input type="checkbox" name="cb1" value="y" /></td>
<td class=label>Click me</td>
</tr>
Then use this CSS:
#cb3.highlight .label {background-color:yellow;}
#cb2.highlight .label {background-color:green;}
#cb1.highlight .label {background-color:red;}
Demo: JSFiddle
I have a HTML code about 5 questions and a "send" button - a message will also show after clicking on it... I would like to add a second button (clear/reset) next to the first one so all previous answers and message would disappear - like a "take-the-test-again" button. How can I accomplish this? Thank you in advance for your answers :)
</head>
<body onload="gophrases()">
<div class="page">
<div class="title">
<h3>Subject</h3>
</div>
<div class="questions">
<table>
<tr>
<td id="num">01.</td>
<td id="quest">Request 1</td>
<td id="ans"><input type="radio" name="q1" onclick="enable('q2')">Yes <input type="radio" name="q1" onclick="enable('q2')">No</td>
</tr>
<tr>
<td id="num">02.</td>
<td id="quest">Request 2</td>
<td id="ans"><input type="radio" name="q2" onclick="enable('q3')">Yes <input type="radio" name="q2" onclick="enable('q3')">No</td>
</tr>
<tr>
<td id="num">03.</td>
<td id="quest">Request 3</td>
<td id="ans"><input type="radio" name="q3" onclick="enable('q4')">Yes <input type="radio" name="q3" onclick="enable('q4')">No</td>
</tr>
<tr>
<td id="num">04.</td>
<td id="quest">Request 4</td>
<td id="ans"><input type="radio" name="q4" onclick="enable('q5')">Yes <input type="radio" name="q4" onclick="enable('q5')">No</td>
</tr>
<tr>
<td id="num">05.</td>
<td id="quest">Request 5</td>
<td id="ans"><input type="radio" name="q5" onclick="enable('q6')">Yes <input type="radio" name="q5" onclick="enable('q6')">No</td>
</tr>
<tr>
</table>
</div>
<div class="submit">
<input type="button" value="Submit" onclick="check()" id="submitbutton">
</div>
<div class="result">
<p><span id="phrase"></span></p>
</div>
<div class="phrases">
<p><span id="score"></span></p>
</div>
</div>
<script type="text/javascript">
function check() {
var count = 0;
var qs = new Array();
qs[0] = document.getElementsByName("q1");
qs[1] = document.getElementsByName("q2");
qs[2] = document.getElementsByName("q3");
qs[3] = document.getElementsByName("q4");
qs[4] = document.getElementsByName("q5");
for(var i = 0 ; i < qs.length; i++) {
if (qs[i][0].checked) {
count++;
}
}
var phrase = "";
if(count < 1) {
phrase = "Bad";
} else if(count < 3) {
phrase = "Good";
} else if(count < 5) {
phrase = "Excellent";
}
document.getElementById("phrase").innerHTML = phrase;
}
function enable(name) {
var radio = document.getElementsByName(name);
if(!radio[0].checked || !radio[1].checked) {
radio[0].disabled = false;
radio[1].disabled = false;
}
}
function button() {
document.getElementById("submitbutton").disabled = false;
}
</script>
</body>
</html>
You can try this:
<button type="reset" value="Reset">Reset</button>
You must wrap the button into a form like this:
<form>
<button type="reset" value="Reset">Reset</button>
</form>
See more about it # w3schools - HTML Button type Attribute
Demonstration.