check box code not working - javascript

<script>
function edit(em) {
var ch = em.value;
var ed = $("td.td" + ch).value;
if ($(ed).is(: checked)) {
$(this).show();
} else {
$(this).hide();
}
}
</script>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<input type="checkbox" value="25" onclick="edit(this)">
<input type="checkbox" value="26" onclick="edit(this)">
<input type="checkbox" value="27" onclick="edit(this)">
<table>
<tr>
<td class="td25" value="25">Edit</td>
<td class="td26" value="26">Edit</td>
<td class="td27" value="27">Edit</td>
</tr>
</table>
</body>
</html>

here is a bug:
if($(ed).is(:checked))...
shoul be:
if($(ed).is(':checked'))...

Following is what you are trying to do.
$('input[type="checkbox"]').click(function() {
var ch = this.value;
if ($(this).is(':checked')) {
$(".td" + ch).hide();
}
else
{
$(".td" + ch).show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<input type="checkbox" value="25" >
<input type="checkbox" value="26" >
<input type="checkbox" value="27" >
<table>
<tr>
<td class="td25" value="25">Edit25</td>
<td class="td26" value="26">Edit26</td>
<td class="td27" value="27">Edit27</td>
</tr>
</table>
</body>
</html>

This is what I think you're trying to do:
function edit(em) {
var ch = em.value;
var ed = $("td.td" + ch);
if ($(em).is(':checked')) {
ed.show();
} else {
ed.hide();
}
}
If that's what you're trying to achieve, then here are things you weren't doing right:
As #yangguang said, you were missing quotes for checked (':checked')
You wanted to take td not its value.
You were checking whether td (or its value) is checked which
instead you should have done it for the checkbox.
$(this) refers to the checkbox, but you wanted to show/hide the td

Related

Javascript JQuery - Hide a button if value is 0, display if not

I have this:
<input type="button" id="total_items" value="0">
The value is increasing as items added to the site.
What I need is to hide it if the value is 0 and start to displaying as the value is increasing.
I know JQuery has an option to add a css display:none option, but I don't know how. Thank you for all the help!
Try this code below, Just you must call checkValue() function every time number count of objects changed.
var btn = $('#total_items');
$(document).ready(function() {
checkValue();
})
btn.change(function() {
checkValue();
});
function AddNumber(count) {
btn.prop('value', parseInt(btn.attr('value')) + count);
checkValue();
}
function checkValue() {
if (parseInt(btn.prop('value')) === 0)
btn.css("display", "none"); //btn.hide();
else
btn.css("display", ""); //btn.show()
console.log("Current value is:" + btn.prop('value'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="total_items" value="0">
<hr>
<button type="button" onclick="AddNumber(1)">+</button>
<button type="button" onclick="AddNumber(-1)">-</button>
if the element with id total_items is fixed then you can use the css3's attribute selector to make the visibility as hidden and then call the increment logic to make it visible again.
Here is a sample snippet handling the visibility of the total_items based on the value it has:
var btntotal_item = document.getElementById('total_items');
function incrementValue() {
btntotal_item.value++;
}
function decrementValue() {
btntotal_item.value--;
if (btntotal_item.value < 0)
btntotal_item.value = 0;
}
#total_items[value="0"] {
visibility: hidden
}
<input type="button" id="total_items" value="0">
<input type="button" onclick="incrementValue()" value="increment by 1"><input type="button" onclick="decrementValue()" value="decrement by 1">
You can also do this using vanilla javascript =>
if no results are equal to 0 then hideShow button will not show up.
Button press will hide results with value 0.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
.noDisplay {
display: none;
}
</style>
</head>
<body>
<button id="p1" class="noDisplay">Hide-Show</button>
<table style="width:100%">
<tr>
<th>name</th>
<th>result</th>
</tr>
<tr id="0">
<td>Jill</td>
<td class="result">0</td>
</tr>
<tr id="1">
<td>Eve</td>
<td class="result">30</td>
</tr>
<tr id="2">
<td>john</td>
<td class="result">0</td>
</tr>
</table>
<script>
window.addEventListener('load', HideShowBtn);
let z = [];
function HideShowBtn() {
let x;
fields = document.getElementsByClassName('result');
// Hack to convert nodelists to array
fieldsArr = Array.prototype.slice.call(fields);
x = fieldsArr.map(e => {
return parseInt(e.innerText);
});
let y = document.getElementById('p1').classList;
function check(x) {
return x <= 0;
}
x.forEach(e => {
if (e === 0) {
y.remove('noDisplay');
}
});
for (const i in x) {
if (x[i] === 0) {
z.push(x.indexOf(0, i));
}
}
}
document.getElementById('p1').addEventListener('click', () => {
z.forEach(e => {
document.getElementById(e).classList.toggle('noDisplay');
});
});
</script>
</body>
</html>
You can achieve this effect with CSS alone.
If you want the button to disappear, but leave a button-sized space, use:
visibility: hidden;
If you want the button to disappear completely, use:
display: none;
If you want the button to be invisible (and still clickable), use:
opacity: 0;
Working Example:
input[type="button"][value="0"] {
visibility: hidden;
}
<input type="button" id="total_items" value="2">
<input type="button" id="total_items" value="1">
<input type="button" id="total_items" value="0">
<input type="button" id="total_items" value="1">
<input type="button" id="total_items" value="2">
Where you set the value
$("#total_items").val(value).toggle(value>0)

Shading a cell in a table when button is clicked

I have a function that shades every other row in a cell, but can't figure out how to get it so when the button is clicked again, the cells get un-shaded. I'd also like it to not select the first row with the row headers. Any ideas would be appreciated!
$(document).ready(function() {
$('#nbrTxt').focus();
function addItem() {
var value = $('#nbrTxt').val();
var usrName = prompt("Name?");
for (var i = 1; i <= value; i++) {
$('table').append('<tr><td></td><td></td></tr>');
$('table tr:last td:first').html(i);
$('table tr:last td:last').html(usrName);
$(this).focus().select();
};
};
$('#btnGo').click(addItem);
$('#nbrTxt').keydown(function(e) {
if (e.which === 13)
addItem();
})
$(document).on('click', '#shade', function() {
$('tr:even').css('background', '#A0A0A0');
})
$(document).on('click', '#drkLine', function() {
if ($('#nbrTxt').val() % 10 === 0) {
($('#nbrTxt').val()).css('textDecoration', 'line-through');
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!doctype html>
<html>
<head>
<title>JQuery Selector</title>
<style type="text/css">
body {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
}
</style>
<script src="jquery-1.11.3.min.js"></script>
<script src="jqueryselector.js"></script>
</head>
<body>
<h1>JQuery Selector</h1>
Enter Number:
<input type="number" name="nbrTxt" id="nbrTxt" />
<input type="button" value="GO" id="btnGo" />
<div id='buttons'>
<input type="button" value="Shade Even Rows" id="shade" />
<input type="button" value="Show Dark Line Every 10 Rows" id="drkLine" />
</div>
<table id="table" width="500" border="1">
<tr>
<td>No. Count</td>
<td>Name</td>
</tr>
</table>
</body>
</html>
Add class shade in your style and use toggleClass() function to add/remove it, check example bellow.
If you don't want to select the first row you can use :not(:first-child) :
$('tr:not(:first-child):even')
Hope this helps.
Snippet
$(document).ready(function() {
$('#nbrTxt').focus();
function addItem() {
var value = $('#nbrTxt').val();
var usrName = prompt("Name?");
$('table>tbody').empty();
for (var i = 1; i <= value; i++) {
$('table').append('<tr><td></td><td></td></tr>');
$('table tr:last td:first').html(i);
$('table tr:last td:last').html(usrName);
$(this).focus().select();
};
};
$('#btnGo').click(addItem);
$('#nbrTxt').keydown(function(e) {
if (e.which === 13)
addItem();
})
$(document).on('click', '#shade', function() {
$('tr:not(:first-child):even').toggleClass('shade');
})
$(document).on('click', '#drkLine', function() {
if ($('#nbrTxt').val() % 10 === 0) {
($('#nbrTxt').val()).css('textDecoration', 'line-through');
}
})
});
tr.shade{
background: #A0A0A0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>JQuery Selector</h1>
Enter Number:
<input type="number" name="nbrTxt" id="nbrTxt" />
<input type="button" value="GO" id="btnGo" />
<div id='buttons'>
<input type="button" value="Shade Even Rows" id="shade" />
<input type="button" value="Show Dark Line Every 10 Rows" id="drkLine" />
</div>
<table id="table" width="500" border="1">
<thead>
<tr>
<td>No. Count</td>
<td>Name</td>
</tr>
</thead>
</table>

Radio buttons group

I have a radio buttons group, and I trying to show button if 5 radio checked, but i cant do that. Here is my code:
$(document).ready(function () {
$('#vote_submit_button').hide();
var unanswered = new Array();
$('table').each(function () {
var question = $(this).find('input').attr('name');
if (!$(this).find('input').is(':checked')) {
unanswered.push(question);
}
});
if (unanswered.length > 0) {
} else {
$('#vote_submit_button').show();
}
});
Help please)
Your code is running on document ready at which point of course all questions will be unanswered.
you need to bind an event handler to the radio buttons
$(function(){
$('#vote_submit_button').hide();
var answered = new Array();
$('input:radio').change(function(){
var $this= $(this);
if ($this.is(':checked')){
anwered.push($this.attr('name'));
}
if (answered.length == $('table').length) {
$('#vote_submit_button').show();
}
});
});
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
$(".radio").click(function(){
$('#vote_submit_button').hide();
var unanswered = new Array();
$('table').each(function () {
var question = $(this).find('input').attr('name');
if (!$(this).find('input').is(':checked')) {
unanswered.push(question);
}
});
if (unanswered.length > 0) {
} else {
$('#vote_submit_button').show();
}
});
});
</script>
<table>
<tr><td>
<input type="radio" name="radio1" class="radio">
</td></tr>
</table>
<table>
<tr><td>
<input type="radio" name="radio2" class="radio">
</td></tr>
</table>
<table>
<tr><td>
<input type="radio" name="radio3" class="radio">
</td></tr>
</table>
<table>
<tr><td>
<input type="radio" name="radio4" class="radio">
</td></tr>
</table>
<table>
<tr><td>
<input type="radio" name="radio5" class="radio">
</td></tr>
</table>
<input type="button" id="vote_submit_button" value="submit" style="display:none;">

Stuck trying to assign JS vars to hidden fields

I've been fighting with this for a couple of days now...need some guidance please.
I have pared down a much bigger form to a "sample" size to demonstrate what I am after.
The area in question is blocked off in a very recognizable area in the calcFees function.
I also tried to get fancy and have the vars self post to the form so they could be seen, but that does not work.
UPDATE: Here is a bit more info as to what I am running into.
//At this point the var regularfee is = 26.5
// (confirmed by console.log(regularfee);)
// I want to assign it to the hidden field with the id="regularfee"
// I have tried both of the following lines:
document.getElementById('regularfee').value=regularfee.value;
// console.log(document.getElementById('regularfee.value')); shows "null"
document.getElementById('regularfee').value=regularfee;
// console.log(document.getElementById('regularfee')); shows "[object HTMLDivElement]"
What am I doing wrong?
END OF UPDATE *****************************
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="multiForm" action="post.php" method="POST" id="app" name="app">
<div id="page1" class="page" style="visibility:visible;">
Name: <input type="text" size="40" name="name1" >
<br><br>
<table border="1" cellpadding="5" width="50%">
<tbody>
<tr>
<td align="center" colspan="3"><strong>Membership Classification</strong></td>
</tr>
<tr><td width="1000">
<input name="paymethod" type="radio" class="pay" id="paypal" value="paypal" />I would like to use PayPal   
<input name="paymethod" type="radio" class="pay" id="check" value="check" />I would like to pay by check
</td>
<td style="width:150px" align="right">Fee
</td>
<td style="width:150px">
</td></tr>
<tr>
<td><input name="memberclass" type="radio" class="membership" id="regular" value="regular"/> Regular Membership</td>
<td align="right"><div id=regularfee></td>
<td><div align="right" id=regselectedfee></td>
</tr>
<tr><td colspan="2" align="right">Total </td>
<td><div align="right" id=total>
</td></tr></tbody>
</table>
<input type="hidden" name="regularfee" id="regularfee" value="">
<input type="hidden" name="regselectedfee" id="regselectedfee" value="">
<input type="hidden" name="total" id="total" value="">
</form>
<br>
<input type="button" id="C1" value="Continue" onClick="showLayer('page2')">
</td></tr>
</table>
</div>
<div id="page2" class="page">
<b>Page 2
<br><br>
<input type="button" id="B1" value="Go Back" onClick="showLayer('page1')">
<input type="submit" name="submit" value="Click to see Vars" />
</div>
</form>
</body>
</html>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script language="JavaScript">
var paypalselected
var checkselected
var regularfee
var memberfee1
var total
$(function () {
function clearForm()
{
paypalselected = "0";
checkselected = "0";
regularfee = 0.0;
memberfee1 = 0.0;
total = 0.0;
$("#regselectedfee").text(memberfee1.toFixed(2));
$("#total").text(total.toFixed(2));
// clear all radio buttons
$("#regular").prop("checked", false );
}
function calcFees()
{
total = (memberfee1);
$("#total").text(total.toFixed(2));
// **********************************************************************************
// Here is where I want to plug in the 3 JS vars to the hidden fields
// regularfee, regselectedfee, total
// Here is what I've tried:
// vars are not getting plugged in
// If possible, I would like the vars to be plugged in dynamically
// just as the form is updateddynamically when user selects buttons
document.getElementById('regularfee').value=regularfee;
document.getElementById('regselectedfee').value=regselectedfee;
document.getElementById('total').value=total;
// **********************************************************************************
}
function selectPayment()
{
$(".pay").change(function () {
clearForm();
if ($("#paypal").prop("checked")) {
regularfee = 26.50;
$("#regularfee").text(regularfee.toFixed(2));
paypalselected = "1";
checkselected = "0";
}
if ($("#check").prop("checked")) {
regularfee = 25.0;
$("#regularfee").text(regularfee.toFixed(2));
checkselected = "1";
paypalselected = "0";
}
});
}
clearForm();
selectPayment();
//start of membership button selection
$(".membership").change(function () {
if (paypalselected == "1"){
if ($("#regular").prop("checked")) {
memberfee1 = 26.5;
$("#regselectedfee").text(memberfee1.toFixed(2));
calcFees();
}
} //end of paypalselected test
if (checkselected == "1"){
if ($("#regular").prop("checked")) {
memberfee1 = 25.0;
$("#regselectedfee").text(memberfee1.toFixed(2));
calcFees();
}
} //end of checkselected test
}); //end of $(".membership").change(function () {
});
//end of main function
var currentLayer = 'page1';
function showLayer(lyr){
hideLayer(currentLayer);
document.getElementById(lyr).style.visibility = 'visible';
currentLayer = lyr;
window.scrollTo(0,0);
}
function hideLayer(lyr){
document.getElementById(lyr).style.visibility = 'hidden';
}
</script>
<style>
body{
font: 10pt sans-serif;
}
.page{
position: absolute;
top: 10;
left: 100;
visibility: hidden;
}
p.small
{
line-height: 5px;
}
p.smalltext12
{
font-size:12px
}
</style>
You have 2 elements #total. Only the first is given a value:
document.getElementById('total').value = total;
Same for the others.
IDs must be unique to be useful.

Button click not working

The following code was working very well. Suddenly, the Edit button click stopped working. When the user used to click Edit button, a JSON code executed. But it is not recognizing the click now. Something happened accidentally? Please assist.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script src="js/calendar.js" type="text/javascript"></script>
<link href="js/calendar.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="css/wysiwyg.css" type="text/css">
<script type="text/javascript" src="js/wysiwyg.js"></script>
<script type="text/javascript" src="js/wysiwyg-settings.js"></script>
<!-- JSON implementation to get data through JQuery/AJAX -->
<script type="text/javascript">
$(document).ready(function(){
$("#Edit").click(function(){
$.getJSON("fetchvalues.php?UpdateRecordID=" + $.cookie('UpdateRecordID'),
function(data){
//Fill the Form with the data values
document.getElementById('LDate').value = data[0];
document.getElementById('Places').value = data[1];
document.getElementById('Company').value = data[2];
document.getElementById('Designation').value = data[3];
document.getElementById('ProjectDetails').value = data[4];
document.getElementById('DesiredCandidate').value = data[5];
document.getElementById('HRName').value = data[6];
document.getElementById('HRContact').value = data[7];
document.getElementById('Email').value = data[8];
});
});
});
</script>
<title>Job Listing Entry</title>
</head>
<body>
<table id="main" cols="2">
<tr>
<td>
<Form id="frmNewEntry" method="post" action="insert_listing.php">
<table id="tblEntry" cols="3" style="border-color:lightblue; border-style:solid;">
<tr><td colspan="3" bgcolor="lightblue" align="center"><strong>Real-Time Vacancy Entry</strong></td></tr>
<tr><td>Date:</td><td><input id="LDate" name="LDate" type="text" size="20" maxlength="11"/>[Select Date from the Calendar Control]
<script type="text/javascript">
WYSIWYG.attach('all', full);
calendar.set("LDate");
</script></td>
<td>
<table>
<tr>
<td rowspan="6">
<!-- <iframe src="show_db_vacancy_entries.php" height="800px" width="300px" bordercolor="cyan">
</iframe> -->
</td>
</tr>
</table>
</td>
</tr>
<tr><td>Places:</td><td><input id="Places" name="Places" type="text" size="35" maxlength="30" onblur="this.value=MakeInitialCapital(this.value);"></td></tr>
<tr><td>Company:</td><td><input id="Company" name="Company" type="text" size="50" onblur="this.value=MakeInitialCapital(this.value);">
<!-- <input type="button" value="Make Initial Capital" align="left" onclick="this.value=MakeInitialCapital(this.value);"></tr> -->
<tr><td>Designation:</td><td><input id="Designation" name="Designation" type="text" size="50" onblur="this.value=MakeInitialCapital(this.value);"></td></tr>
<tr><td>Project Details:</td><td><textarea id="ProjectDetails" name="ProjectDetails" cols="100" rows="10"></textarea></td></tr>
<tr><td>Desired Candidate:</td><td><textarea id="DesiredCandidate" name="DesiredCandidate" rows="3" cols="100"></textarea> <br></td></tr>
<tr><td>HR Name:</td><td><input id="HRName" name="HRName" type="text" size="50" onblur="this.value=MakeInitialCapital(this.value);"> <br></td></tr>
<tr><td>HR Contact:</td><td><input id="HRContact" name="HRContact" type="text" size="50"> <br></td></tr>
<tr><td>Email:</td><td><input id="Email" name="Email" type="text" size="50"> <br></td></tr>
<tr></tr>
<tr>
<td bgcolor="lightblue">
<input id="Clear" name="Clear" value="Clear" type="button" onclick="ClearFields();">
</td>
<td bgcolor="lightblue">
<input id='Submit' name='Submit' value='Submit' type='submit' />
</td>
</tr>
</table>
</Form>
</td>
<td>
<table id="list" cols="2" style="border:none">
<tr>
<td colspan="2" style="border:none">
<iframe src="show_db_vacancy_entries.php" height="600px" style="border:none;">
</iframe>
</td>
</tr>
<tr>
<td align="left">
<input id="Edit" name="Edit" value="Edit Record" type="button" />
</td>
<td align="right">
<input id="Delete" name="Delete" value="Delete" type="button" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<script language="JavaScript" type="text/javascript">
function MakeInitialCapital(str)
{
return str.toLowerCase().replace(/\b[a-z]/g, cnvrt);
function cnvrt() {
return arguments[0].toUpperCase();
}
}
//Convert initials to capital in a certain control
function MakeInitialCapitalControl(controlName)
{
var ctrl = document.getElementById(controlName).value;
if(/^[A-Z]/.test(ctrl.value)) {
ctrl.value = ctrl.value.toLowerCase();
return;
}
/* ctrl.value = ctrl.value.toLowerCase().replace(/\b[a-z]/g, function {
return arguments[0].toUpperCase();
});*/
}
function ClearFields()
{
document.getElementById('Email').value = "";
document.getElementById('HRContact').value = "";
document.getElementById('HRName').value = "";
document.getElementById('DesiredCandidate').value = "";
document.getElementById('ProjectDetails').value = "";
document.getElementById('Designation').value = "";
document.getElementById('Company').value = "";
document.getElementById('Places').value = "";
document.getElementById('LDate').value = "";
}
</script>
I've tested the code, after removing all the external JS and CSS files, and it seems to work fine.
The problem is most likely with the JSON data that you are getting back. Perhaps you did something to the PHP file it is calling, so that the JSON object is malformed, or there is a PHP warning being printed. (Could be a result of a change in the PHP configuration, too)
I would suggest that you try using the page in Firefox with the Firebug addon active (or something equivalent). It will show you exactly what the JSON request is returning, and whether there are any errors with the request.
As Atli said, the code looks fine. Im curious about this $.cookie('UpdateRecordID'), being part of the querystring. What happens if the cookie is not set?
Can you verify a proper response via firebug?

Categories

Resources