Javascript not accepting my function arguments? - php

i am passing the class and id of chechboxes to the js function and try to make it dynamic to work for id and class of any checkbox to check all and uncheck all check boxes
here is the html
<input type="checkbox" name="reply_status[]" <?=$checked;?> class="memebers" value="<?=$status[$e]['mail_sent_id']?>">
</td>
<td>
<input type="checkbox" name="send_invoice[]" <?=$checked_send_invoice;?> class="send_invoice" value="<?=$status[$e]['mail_sent_id']?>">
</td>
<td>
<input type="checkbox" name="invoice_paid[]" <?=$checked_invoice_paid;?> class="invoice_paid" value="<?=$status[$e]['mail_sent_id']?>">
</td>
<td>
<input type="checkbox" id="check_all_re" name="check_all" onclick="checkall_members('memebers','check_all_re')">
Check All
</td>
<td>
<input type="checkbox" id="check_all_sv" name="check_all" onclick="checkall_members('send_invoice','check_all_sv')">
Check All
</td>
<td>
<input type="checkbox" id="check_all_ip" name="check_all" onclick="checkall_members('invoice_paid','check_all_ip')">
Check All
</td>
and here is the js function
<script type="text/javascript">
function checkall_members(chechboxclass, chechboxid) {
// var id = document.getElementById("check_all");
alert();
chechboxid = $('#' + chechboxid);
chechboxclass = $('.' + chechboxclass);
if (chechboxid.checked) {
chechboxclass.attr('checked', 'checked');
}
else {
chechboxclass.removeAttr('checked');
}
}
any help would be appreciated

Replace your if..else block with the following.
chechboxclass.prop('checked', chechboxid.prop('checked'));
By the way, it's checkbox and not chechbox.
To improve your code even more, get rid of the inline events:
$('input[name="check_all"]').on('change', function() {
$($(this).data('toggleAll')).prop('checked', this.checked);
});
Now you just need to add data-toggle-all=".send_invoice" etc. to those checkboxes. The param value should be the selector of the checkboxes you want to toggle with that checkbox.
If you have all the name="check_all" elements a class, you could also speed up the selector by using input.whatever_class instead of input[name="check_all"]

i have sorted out the simplest solution for my problem that is
<script type="text/javascript">
function checkall_members(chechboxclass, chechboxid) {
var checkbox = document.getElementById(chechboxid);
chechboxclass = $('.' + chechboxclass);
if (checkbox.checked) {
chechboxclass.attr('checked', 'checked');
}
else {
chechboxclass.removeAttr('checked');
}
}
</script>
i have to get the id elememt within same library of javascript thats why it was not working
var checkbox = document.getElementById(chechboxid);
thanks guys for helping me

Related

convert checked rows to input fields

I am showing data from database into table with the checkbox in front of every row. All I want is to change the checked rows columns with class click into input fields on clicking the edit button. I am very new to jQuery. I don't have much concepts about it. So help me please.
Here is the jQuery code I've tried:
$(function () {
$("input[name='check[]']:checked").each(function (){
$('#edit').click(function(){
var OriginalContent = $('.click').text();
$('.click').addClass("cellEditing");
$('.click').html("<input type='text' value='" + OriginalContent + "' />");
$('.click').children().first().focus();
$('.click').children().first().keypress(function (e) {
if (e.which == 13) {
var newContent = $('.click').val();
$('.click').parent().text(newContent);
$('.click').parent().removeClass("cellEditing");
}
});
$('.click').children().first().blur(function(){
$('.click').parent().text(OriginalContent);
$('.click').parent().removeClass("cellEditing");
});
});
});
});
And here is the html:
echo '<div class="table-responsive">';
echo '<table class="table table-bordered table-hover"><thead><tr>
<th>ID</th><th>Name</th><th>Category</th><th>Description</th><th>Price</th><th>Status</th><th colspan="2">Image</th></tr></thead><tbody>';
while($row = mysqli_fetch_array($retval))
{
echo '<tr><td>'.$row["ID"].'</td>
<td >'.$row["Name"].'</td>
<td>'.$row["Category"].'</td>
<td>'.$row["Description"].'</td>
<td class="click1">'.$row["Price"].'</td>
<td class="click">'.$row["Status"].'</td>
<td><img style="width: 3em; heigth: 3em;" class="img-responsive" src="'.$row["Imagepath"].'" alt="'.$row["Name"].'"/></td>
<td><input class="checkbox" name="check[]" id="check[]" type="checkbox" value='.$row["ID"].'/></td></tr>';
}
echo '</tbody></table>';
echo '</div>';
?>
<center><input class="btn btn-default" name="deletebutton" type="submit" value="DELETE" />
<input class="btn btn-default" name="edit" id="edit" type="button" value="EDIT" />
<input class="btn btn-default" name="updatebutton" type="submit" value="UPDATE"/></center>
</form>
Basically you need two functions to run...
when "edit" is clicked: convert all "editable" columns to INPUTs, if that row is ":checked"
when ENTER is pressed while editing one of your "editable" INPUTs: revert it to a string
I simplified your HTML a bit for clarity. An explanation of the jQuery needed is inline.
Also, here's a working jsFiddle: http://jsfiddle.net/msz76tgp/2/
HTML
<table>
<tr>
<td class="editable status">something</td>
<td class="editable price">2</td>
<td class="checkbox"><input type="checkbox"/></td>
</tr>
<tr>
<td class="editable status">something else</td>
<td class="editable price">3</td>
<td class="checkbox"><input type="checkbox"/></td>
</tr>
</table>
<a id="edit" href="#edit">edit</a>
jQuery
$(function () {
// When edit is clicked...
$('#edit').on('click', function(){
// For each ":checked" checkbox...
$('.checkbox INPUT:checked').each( function(){
// Get the parent row...
var $row = $(this).closest('tr');
// And that row's "editable" columns...
var $editable= $row.children('.editable');
// Add the "editing" class...
$editable.addClass('editing');
// And change all strings to INPUT fields.
$editable.each( function(){
$(this).html('<input value="'+$(this).text()+'"/>');
});
});
});
// Also...
// Any time an .editing INPUT receives a keypress...
$('table').on('keypress', '.editing', function(e){
// If the key pressed is ENTER...
if( e.which == 13 ){
// Get the current row...
var $row = $(this).closest('tr');
// Get the editable columns...
var $editable = $row.children('.editable');
// Remove the class...
$editable.removeClass('editing');
// Change back to string...
$editable.each( function(){
$(this).html( $(this).find('INPUT').val() );
});
}
});
});
I didnt really use your code, since you're writing html with php which is hard to maintain and isnt my preferred style (id rather write html and then put php inside) but this should get help you with your question: http://jsfiddle.net/swm53ran/28/.
comments in the code show what's going on. in essence, im using jquery to add the input with the data that is already in there. but since you are using php, you can just modify the code below by inserting <?php echo();?> inside the html code built in the javascript section.
<tr>
<td class="name">php echo name here</td>
<td class="description">php echo description here</td>
<td class="data">php echo data here</td>
<td>Edit Info</td>
</tr>
$('.edit').on('click', function() {
var row = $(this).closest('tr');
***NOT NECESSARY SECTION OF CODE BECAUSE YOU'RE USING PHP (JUST FOR DEMO)****
//collect data thats there (you dont have to do this if you have php,
//then you can just code in <?php echo();?> into your html building in
//javascript) because i have no php here
var name = row.find('.name').html();
var description = row.find('.description').html();
var data = row.find('.data').html();
***END OF NOT NECESSARY SECTION****
//construct inputs to be inserted into cells
//since you're using php, you modify the below code to something like this:
//var nameInput = '<input type="text" value="<?php echo('name');?>"/>';
//but you have to be careful because ^ this code wont work because of the
//nested parenthesis escape each other, so just put like <?php echo('name');?>
//into another variable and stick the variable in instead.
var nameInput = '<input type="text" value="'+ name +'"/>';
var descriptionInput = '<input type="text" value="'+ description +'"/>';
var dataInput = '<input type="text" value="'+ data +'"/>';
//remove all contents and append inputs
row.find('.name').html('').append(nameInput);
row.find('.description').html('').append(descriptionInput);
row.find('.data').html('').append(dataInput);
});
hope this helps!

How to make dynamic list transferring "unknown" id's work with static script

Note #1: var id = $("#mysqlid").val(); is supposed to be the value from the hidden field from the form where the delete button has been pushed.
The id is unknown, since it is the auto_increment id from the ever changing database. To make it unique.
<script type="text/javascript">
$(document).ready(function(){
$(document).on("click", "#delete", function(){
var id = $("#mysqlid").val(); // Note #1
$.ajax({
type:"post",
url:"http://www.mydomain.no/action.php",
data:"id="+id+"&action=delete",
success:function(data){
showList();
// showlist() returns an updated list of entries from the database after the selected row is deleted.
}
});
});
});
</script>
<table id="list">
<theader>
<tr>
<td>Name</td><td>Action</td>
</tr>
</theader>
<tbody>
<!-- Content from action.php start here -->
<tr>
<td>Test data</td>
<td>
<form>
<input type="hidden" id="mysqlid13" value="13">
<a href="#" id="delete">
<img style="cursor:pointer;" src="gfx/delete.png" alt="Delete">
</a>
</form>
</td>
</tr>
<tr>
<td>Test data 2</td>
<td>
<form>
<input type="hidden" id="mysqlid14" value="14">
<a href="#" id="delete">
<img style="cursor:pointer;" src="gfx/delete.png" alt="Delete">
</a>
</form>
</td>
</tr>
<!-- Content from action.php ends here -->
</tbody>
</table>
I've removed all the excess code, as to not clutter the issue at hand.
I'm sure this is an easy fix for someone more advanced in javascript than me.
As per my previous suggestion, you can assign a common class name to the elements that will need to be grabbed and passed back to the server. The class name wouldn't have to be unique just known. If you give all the elements you wish to perform this action on the same class name you can select those elements, get their values, and pass them back in your ajax call.
Given html:
<form>
<input type="hidden" id="mysqlid13" class='deleteMe' value="13">
<a href="#" id="delete">
<img style="cursor:pointer;" src="gfx/delete.png" alt="Delete">
</a>
</form>
Your js could become:
$(document).on("click", "#delete", function(){
var id = $(this).closest("form").find(".deleteMe").val();
/*make your ajax call or whatever*/
});
Now if you only have that one hidden input element per form element you could even simplify it and not even use an identifier and select by type:
$(document).on("click", "#delete", function(){
var id = $(this).closest("form").find("input[type='hidden']").val();
/*make your ajax call or whatever*/
});
I'd prefer to do this in a bit better approach, like this
Note it's invalid HTML to have duplicate ID in a document, please change it to class
<form>
<input type="hidden" name="id" value="14" />
<input type="hidden" name="action" value="delete" />
<a href="#" class="delete">
<img style="cursor:pointer;" src="gfx/delete.png" alt="Delete" />
</a>
</form>
$(document).on("click", ".delete", function (e) {
e.preventDefault(); // prevent default action
var that = this;
$.ajax({
type: "post",
url: "http://www.mydomain.no/action.php",
data: $(that).closest('form').serialize(),
success: function (data) {
showList();
// showlist() returns an updated list of entries from the database after the selected row is deleted.
}
});
});
set data format , it may not pass like query string and complete url may not work cause of CORS
$.ajax({
type:"post",
url:"/action.php",
data:{"id":id,"action":"delete"},
success:function(data){
showList();
}
First you need to change you links to use a class, not an id, as ids must be unique. So you html becomes
<a href="#" class="delete">...
Then your js becomes
$(document).on("click", ".delete", function(){
var id = $(this).prev("input").val();
...
}

JQuery - checkboxes dont't turn on

I'm having a problem with jquery.
I have a checkbox with ID "#checkAll" and checkboxes with class "checkboxes".
I'd like to turn all checkboxes on when the checkbox with ID "checkAll" is checked,
and I'd like to turn all checkboxes off when the checkbox with ID "checkAll" is checked off.
But, What's having now is that when I first click the checkbox(#checkAll), all other checkboxes are turned on and when I first turn the ckeckbox(#checkAll), all other checkboxs are turned off as well.
But, when I try to click the checkbox(#checkAll) in the second time, all other checkboxes are not turned on.
I don't know why this happens.
Please help me out!
This is my javascript handling events.
ADMIN.event = (function() {
function _init(){
$(function(){
var checkAll = $('#checkAll'),
checkboxes = $('.checkboxes');
checkAll.click(function(){
if($(this).is(':checked')){
checkboxes.attr('checked', 'checked');
} else {
checkboxes.removeAttr('checked');
}
})
});
}
_init();
}());
This is my view file in php.
<table class="fullTable">
<tr class="listTableTr">
<th class="listTableTh tinyTh"><input type="checkbox" name="" id="checkAll" /></th>
<th id="articleTitleTh" class="listTableTh">title</th>
</tr>
<?php foreach($postData as $post): ?>
<td class="listTableTd"><input type="checkbox" name="" class="checkboxes" /></td>
<td class="listTableTd"><?php echo $post['title'];?></td>
<?php endforeach; ?>
</table>
Try with .prop() like
checkAll.click(function(){
checkboxes.prop('checked',this.checked);
});
And its better to use even with .on() like
checkAll.on('click' , function(){
checkboxes.prop('checked',this.checked);
});
Try this :
$('#checkAll').on('click',function(){
if($("#checkAll").is(':checked')) {
$('.checkboxes').attr('checked',true);
} else {
$('.checkboxes').attr('checked',false);
}
});
DEMO
Try this
$("#checkAll").click(function () {
if ($(this).is(":checked"))
$(".checkboxes").prop("checked","checked");
else
$(".checkboxes").prop("checked",false);
});

check all checkboxes

I have a table with checkboxes and I want to do the "check all" and "un-check all" checkboxes but I could not find the way to check all the checkboxes.
Here is my code:
<form>
<?php foreach ($this->entries as $entry): ?>
<tr class="table_head_seperator">
<td class="grid_info" width="32px" bgcolor="#f6f6f6"><input type="checkbox" class="chk_boxes1" name="to_delete[<?PHP echo $entry['id'] ?>]" /></td>
<td class="grid_info" width="160px" bgcolor="#eeeeee"><span class="country_name"><?php echo $entry['user_name'] ?></span></td>
<td class="grid_info" width="130px" bgcolor="#eeeeee"><span class="country_name"><?php echo $entry['date_created'] ?></span></td>
<td class="grid_info" width="100px" bgcolor="#f6f6f6"><span class="country_name"><?php echo $entry['user_type_name'] ?></span></td>
</tr>
<?PHP endforeach; ?>
<input type="checkbox" class="chk_boxes" label="check all" />check all
<input type="checkbox" class="unchk_boxes" /> un-check all
</form>
<script type="text/javascript">
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',checked)
})
});
</script>
$(function() {
$('.chk_boxes').click(function() {
$('.chk_boxes1').prop('checked', this.checked);
});
});
This only affects the check all box. But why would you want to use two checkboxes anyway? One to check all and one to uncheck all, but not mutually exclusive. That's got to be the recipe to confuse users :)
Try using true|false. Like so:
$('.chk_boxes1').attr('checked',true);
Additional comment:
In addition, it appears your un- and check all checkboxes are redundant.
<input type="checkbox" class="chk_boxes" label="check all" />check all
<input type="checkbox" class="unchk_boxes" /> un-check all
Rather than doing that, you can just have one checkbox that does both. Thus, your jQuery will look like:
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',$(this).attr('checked'));
})
});
With HTML:
<input type="checkbox" class="chk_boxes" label="check all" />check all
See the working solution here http://jsfiddle.net/HBGzy/
you don't need to use the "uncheck all" checkbox :)
$('.chk_boxes').click(function(){
var chk = $(this).attr('checked')?true:false;
$('.chk_boxes1').attr('checked',chk);
});
You forgot about quotes:
$('.chk_boxes1').attr('checked','checked')
fiddle: http://jsfiddle.net/8ZHFn/
Try this
$(".chk_boxes").click(function()
{
var checked_status = this.checked;
$(".chk_boxes1").each(function()
{
this.checked = checked_status;
});
});
So, if the class name of all the checkboxes you want to affect when checking or unchecking the checkbox with class chk_boxes,is chk_boxes1 this will get all the elements and set the checked property accordingly.
Check-uncheck all / select-unselect all checkboxes
Follow the following code , its really simple with the following code
inside your html form add the following line of code
<input type="checkbox" id='checkall' /> Select All
and than add the following script
<script type='text/javascript'>
$(document).ready(function(){
// Check or Uncheck All checkboxes
$("#checkall").change(function(){
var checked = $(this).is(':checked');
if(checked){
$(".checkbox").each(function(){
$(this).prop("checked",true);
});
}else{
$(".checkbox").each(function(){
$(this).prop("checked",false);
});
}
});
// Changing state of CheckAll checkbox
$(".checkbox").click(function(){
if($(".checkbox").length == $(".checkbox:checked").length) {
$("#checkall").prop("checked", true);
} else {
$("#checkall").removeAttr("checked");
}
});
});
</script>
keep in mind that .checkbox in script code is the class name from other checkboxes of html form
for example if you have input checkbox as following
<input class="checkbox" name="emailid[]" type="checkbox" value="<?php echo $email;?>" />
i think you should use the true or false for the checkbox attribute.
<script type="text/javascript" charset="utf-8">
$('document').ready( function() {
$('.chk_boxes').click( function() {
$(':checkbox').attr('checked',true);
});
});
</script>
An advice about check all/uncheck all..
As my think after selecting check all, if user clicks any of checkbox1s, the check all checkbox should be unchecked and also without clicking check all box, if user selects all checkbox1s one by one the checkbox should be selected. So i suggest you to try this when using check all/uncheck all..
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',$(this).attr('checked'));
})
$('.chk_boxes1').click(function(){
if($('.chk_boxes1').length == $('.chk_boxes1:checked').length)
$('.chk_boxes').attr('checked',checked)
else
$('.chk_boxes').attr('checked',false)
})
});

move selected item from one selectbox to another selectbox(with duplicate prevention)

i have two select box, now what i want is
i want to move option from one select box to another via a button
below is my code:
php
<table>
<tr>
<td>
<select size="5" id="suppliedMovies" name="suppliedMovies">
<option selected="selected" value="">Select Movie</option>
<option value="1">sholay</option>
<option value="3">Jism</option>
<option value="4">Rog</option>
<option value="5">Zeher</option>
<option value="6">Awarpan</option>
<option value="7">Paap</option>
<option value="8">paanch<option>
<option value="9">no entry</option>
</select>
</td>
<td>
<input type="button" id="toRight" value=">>"><br>
<input type="button" id="toLeft" value="<<">
</td>
<td>
<select size="5" id="accquiredMovies" name="accquiredMovies">
<option> No item right now</option>
</select>
</td>
</tr>
</table>
Jquery
jQuery(document).ready( function() {
function displayVals() {
var myValues = jQuery("#suppliedMovies").val();
return myValues;
}
jQuery('#toRight').click(function(){
var x=displayVals();
console.log(x);
var txt=jQuery("#suppliedMovies option[value='"+x+"']").text();
console.log(txt);
if(x!=''){
jQuery('#accquiredMovies').append("<option value='"+x+"' >"+txt+"</option>");
}
});
});
i m using above jQuery, that is working fine but, i want that
when one item is copy from one select box to another select box, then that item should be disable(or probably delete) from first select box (to prevent duplicate entry).
i also want to move item from right to left select box
please suggest me optimized jQuery .
Thanks.
UPDATE
if i want to use multiple select box on both side? then how do i use that?
more update
if i click on a item on rightselectbox and move it to left selectbox,
and go further(post) then on right selectbox, there is nothing selected items??
what i need is on right selectbox, there all items shoud be always selected ,
otherwise what i need to do? after i move an item from right to left ,i again have to select rest of items on right selectbox and go further
solution for my update part
i used below code, please take a look and suggest me if somebody finds any mistake/error in this. Thanking You
jQuery('form').submit(function() {
jQuery('#accquiredMovies option').each(function(i) {
jQuery(this).attr("selected", "selected");
});
});
You could use jQuery remove. Like:
$('#suppliedMovies option[value=' + x ']').remove()
..fredrik
EDIT:
You could optimize the function like this:
jQuery(document).ready( function() {
# Store the jQuery object return by the selector so that you don't need to
# make a new selector request next time a user press the #toRight
var suppliedSelect = jQuery('#suppliedMovies');
jQuery('#toRight').click(function () {
suppliedSelect.find(':selected').each(function (index, elem) {
var selectElem = $(elem);
if (selectElem.val()) {
selectElem.val.appendTo('#accquiredMovies');
}
});
});
});
function SelectMoveRows(SS1,SS2)
{
var SelID='';
var SelText='';
// Move rows from SS1 to SS2 from bottom to top
for (var i=SS1.children().length - 1; i>=0; i--)
{
if (SS1.children()[i].selected == true)
{
SelID=SS1.children()[i].value;
SelText=SS1.children()[i].text;
SS2.append($("<option/>", {value: SelID, text: SelText}));
$(SS1.children()[i]).remove();
}
}
SelectSort(SS2);
}
function SelectSort(SelList)
{
// alert("in secod "+(SelList.children().length-1))
var ID='';
var Text='';
for (var x=0; x < SelList.children().length-1; x++)
{
// alert(SelList.children()[x].text)
for (var y=x + 1; y < SelList.children().length; y++)
{
if ($(SelList.children()[x]).val() > $(SelList.children()[y]).val())
{
// Swap rows
alert("x "+SelList.children()[x].value +" y = "+SelList.children()[y].value)
ID=$(SelList.children()[x]).val();
Text=$(SelList.children()[x]).text();
$(SelList.children()[x]).val($(SelList.children()[y]).val());
$(SelList.children()[x]).text($(SelList.children()[y]).text());
$(SelList.children()[y]).val(ID);
$(SelList.children()[y]).text(Text);
// SelList.children()[x].text= SelList.children()[y].text;
// SelList.children()[y].value=ID;
// SelList.children()[y].text=Text;
}
}
}
}
This is also working for moving items from one multi select box to another without duplicating. I wana know how to make order of values same while moving items from one to another select box.
Here's a good example:
HTML:
<form>
<select id="t1available" size=10 multiple><option>Project 1</option><option>Project 2</option><option>Project 4</option></select>
<br />
<input id="t1add" type="button" value=">" />
<input id="t1addAll" type="button" value=">>" />
<input id="t1removeAll" type="button" value="<<" />
<input id="t1remove" type="button" value="<" />
<br />
<select id="t1published" size=10 multiple><option>Project 3</option></select>
</form>
Jquery:
<script type="text/javascript">
function moveSelectedItems(source, destination){
var selected = $(source+' option:selected').remove();
var sorted = $.makeArray($(destination+' option').add(selected)).sort(function(a,b){
return $(a).text() > $(b).text() ? 1:-1;
});
$(destination).empty().append(sorted);
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#t1add').click(function(){
moveSelectedItems('#t1available', '#t1published');
});
$('#t1addAll').click(function(){
$('#t1available option').attr('selected', 'true');
moveSelectedItems('#t1available', '#t1published');
});
$('#t1remove').click(function(){
moveSelectedItems('#t1published', '#t1available');
});
$('#t1removeAll').click(function(){
$('#t1published option').attr('selected', 'true');
moveSelectedItems('#t1published', '#t1available');
});
});
</script>
This example is entirely from the following article, which, unfortunately for English speakers, is in German: Move selected items between checkboxes.
We can do like this:
$('div.buttons').on('click','#add',function(e){
e.preventDefault();
$("#sourceid option:selected").appendTo("#destinationid");
});

Categories