How to dynamically check/uncheck html checkbox by JavaScript in asp.net - javascript

I know that several questions about this topic have been asked, but I was unable to find an answer for my case.
I have gridview. In that Checkbox.
Here, I want to do functionality like When I check on exp_id 1515001101 then others all exp_id 1515001101 get automatically check.
function checkUncheckHeaderCheckBoxforSubmit(obj)
{var checkboxCell;var expidCell;
var grid = document.getElementById("<%=grdViewLocalConvence.ClientID %>");
for (i = 1; i < grid.rows.length - 2; i++)
{ for (i = 1; i < grid.rows.length - 2; i++)
{ checkboxCell = grid.rows[i].cells[8];
expidCell = grid.rows[i].cells[0];
var exp_id = expidCell.innerText;
for (j = 0; j < checkboxCell.childNodes.length; j++)
{if (checkboxCell.childNodes[j].type == "checkbox")
{ if (checkboxCell.childNodes[j].checked == true)
{ var exp_id = expidCell.innerText;
GridVwHeaderChckbox.rows[i].cells[8].getElementsByTagName("INPUT")[0].checked =
exp_id.checked;
}}}}
Please help me.
Thanks.

try this
$(".checkboxid").change(function() {// the first checking checkbox
if(this.checked) {
$('.checkboxid').attr('checked', true);// here put change checkbox details that are checked automatically
}
});
If you want to fix the state of these check boxes remains same then best idea is save data to db and retrieve each time the page load happends

Related

How to clear previous content from a table cell

I'm trying to add a "clearing" function to my table that calculates totals. So that when person first time presses button that does the calculation, then changes amounts of products and then presses again, the previous answer would be cleared and new added.
I have tried like this:
function clear () {
var table = document.getElementById("pricetable");
var rows = table.getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++) {
rows[i].className = "";
var cells = rows[i].getElementsByTagName("td");
for (var j = 1; j < cells.length - 1; j++) {
cells[j].className = "";
}
}
}
Then I'm calling the function in the beginning of my previous function that calculates the amounts and prices:
function calculate () {
clear ();
...
}
But nothing happens. I was thinking that it might have something to do with the fact that I have created the last row and also the last column (which both include the totals) dynamically. The id of the row is lastRow, and the column doesn't have id.
And I don't want to use jquery or add classes, ids etc to the html file. So does anyone know what's wrong with my code?
className just clears styling.
You're looking for innerHTML:
...
for (var j = 1; j < cells.length - 1; j++) {
cells[j].innerHTML = "";
}
...
className refers to the CSS class name(s) applied to an element. Here's what your current code does:
Before
<td class='foo'>999</td>
After
<td class=''>999</td>
innerHTML pretty much does what it says:
Before
<td class='foo'>999</td>
After
<td class='foo'></td>
Also, I just noticed your for loop starts at 1. Hopefully this was intentional ;)
I can see that you are setting the className to nothing rather than setting the innerHTML to nothing...
Try replacing this:
cells[j].className = "";
With this:
cells[j].innerHTML = "";

Issues in setting cookies for store form input values in javascript

I took below code from online,
var myValues = [];
for(i=0;i<inputs.length;i++) {
validateEmail(inputs[i]);
myValues.push(inputs[i].value);
}
// Save them fot later use
localStorage.myValues = JSON.stringify(myValues);
// After clicking the button you can retrieve them back
var oldValues = JSON.parse(localStorage.myValues);
I created simple asp page,
Now, If i click the next after after enter some input value, it will go to next page, and again i click back, it doesn't show entered input values.
I need to show that values, may i know, how can i achieve this one thanks in advance.
Here is the worked jsfiddle
I need to add with my existing javascript code for achieving my need.
But i just confused, how to add code?
Can anyone help me? Thanks in advance.
On page load, you must add the following code:
var $inputs = $(".input");
var myValues = localStorage.getItem("myValues") ? JSON.parse( localStorage.getItem("myValues") ) : [];
for(var i = 0; i < myValues.length; i++) {
$inputs[i].value = myValues[i];
}
This will make sure that on page load, if any data is present in the localStorage it will be populated in the respective input fields.
Now when you click on next button:
$("#nextbutton").click(function () {
var myValues = [],
$inputs = $('.input');
for (i = 0; i < $inputs.length; i++) validateEmail($inputs[i]);
if ($('.invalid').length !== 0) return false;
for (i = 0; i < $inputs.length; i++) {
myValues.push($inputs[i].value)
}
localStorage.myValues = JSON.stringify(myValues);
});
This is used to store the data into the localStorage after validation.

html table selected cell using javascript

I have generated a dynamic html table using java script. Now I need to get the css class of the cell clicked by the user.
How am I supposed to do this?
Here is the code:
function generateSeatMatrix() {
var rows = parseInt(document.getElementById('txtRows').value);
var cols = parseInt(document.getElementById('txtCols').value);
if (!validateMatrixInput(rows, cols)) {
// TODO display error message
return;
}
var matrixTable = document.getElementById('tblMatrix');
if (matrixTable.rows.length > 0) {
for (var k = matrixTable.rows.length - 1; k >= 0; k--) {
matrixTable.deleteRow(k);
}
}
for (var i = 0; i < rows; i++) {
matrixTable.insertRow(i);
for (var j = 0; j < cols; j++) {
matrixTable.rows[i].insertCell(j);
matrixTable.rows[i].cells[j].className = 'matrix-cell';
matrixTable.rows[i].cells[j].setAttribute('onclick', 'hello()');
}
}
}
The 'hello()' function is supposed to handle the required logic. Right now, the function is called properly but I have no idea how to get the selected cell css class. Actually, i tried to send the position as declaring the event (using setAttribute), but then an error raised.
I see you are trying to add a onclick as you built but if you create some jquery like so you can do it. But you would have to have jQuery loaded. Although if you are building this dynamically you may need to use $('document').find('td'). This may not be the solution you are looking for but will get the job done.
$('td').on('click', function(){
var myClass = this.className;
})
Tried adding in comment but wouldn't show code view.
Here is how easily this could be handled..
matrixTable.rows[i].cells[j].setAttribute('onclick', 'hello(' + i + ',' + j + ')');
Well done!

Adding many dropbox choosers with different extensions option does not work

I would like to put different choosers to one page with different extensions options. So I inserted a <div class="dropbox-chooser"></div> to the different positions in my site. Then I wrote the following function to my head(for testing):
window.onload = function() {
if (document.getElementsByClassName("dropbox-chooser")) {
for(var i = 0; i < document.getElementsByClassName("dropbox-chooser").length; i++){
if (i === 0) {
options.extensions = ['.zip','.exe','.rar','.7zip','.php','.html','.css','.js'];['.3gp','.3gpp','.3gpp2','.avi','.mov','.mp4','.wmv','.m4v','.mpg','.mkv','.mpeg','.vob','.flv','.mts','.m2t','.ts','.dv'];
button = Dropbox.createChooseButton(options);alert(options.toSource());
document.getElementsByClassName("dropbox-chooser")[i].appendChild(button);
}
if (i === 2) {
options.extensions = ['.3gp','.3gpp','.3gpp2','.avi','.mov','.mp4','.wmv','.m4v','.mpg','.mkv','.mpeg','.vob','.flv','.mts','.m2t','.ts','.dv'];
button = Dropbox.createChooseButton(options);alert(options.toSource());
document.getElementsByClassName("dropbox-chooser")[i].appendChild(button);
}
}
}
}
and the two choosers are rendered in the right place but when I click one of the chooser buttons, the extensions option is always the last one (.3gp...) although the options.toSource() function shows the right object content for every button.
Can you please help me?
Thanks a lot!
Dennis
I believe the Chooser doesn't look at the options object until the Chooser is actually opened.
You should just pass in a different object each time.
EDIT
Here's what I would suggest:
var choosers = document.getElementsByClassName('dropbox-chooser');
for (var i = 0; i < choosers.length; i++) {
var options = {
// ...
};
if (i === 0) {
options.extensions = ['.zip', '.exe' /* ... */];
} elif (i === 2) {
options.extensions = ['.3gp', '.3gpp' /* ... */];
}
choosers[i].appendChild(Dropbox.createChooseButton(options));
}

How Can I Check the checkbox that belongs a GridVIew?

I want to do a javascript function that check the unchecked checkbox. My function nowadays, check all the unchecked checkbox, and I need that just check a specific GridView unchecked checkbox
function checar() {
var el = document.getElementsByTagName("input");
for (var i = 0; i < el.length; i++) {
if (el[i].type == "checkbox") {
el[i].checked = true;
}
}
}
You want to first limit the scope of your search for elements. One way to do so would be to use getElementById
function checar() {
var grd = document.getElementById("<%=grd.ClientID%>"); // <-- Add this line
var el = grd.getElementsByTagName("input"); // <-- change the scope of this to grd
//rest of your code here.
}
Sample using divs, but you'll get the idea I think: http://jsfiddle.net/8LRkk/
Edited to include setting the specific Grid ID.
To get at all of the checkboxes of a particular gridview, you need to grab checkboxes whose ClientID contain the portion of the gridview's ClientID as well since all controls have an id which is "stacked".
Your function should work as a base, it just needs to have an added check in it:
function checar() {
var el = document.getElementsByTagName("input");
// Get the client id of the gridview from ASP here
var gvID = '<%= this.myGridview.ClientID %>';
for (var i = 0; i < el.length; i++) {
// Call the id of the checkbox and check to see if it
// contains the client id of the gridview at the same time
if (el[i].type == "checkbox" && el.id.indexOf(gvID) != -1) {
el[i].checked = true;
}
}
}

Categories