Insert checkbox selected value to textbox in asp.net? - javascript

I have 5 asp.net checkboxes in webform ...i want as the checkboxes checked and unchecked then its text will be added in textbox i.e if checkbox 1 and checkbox9 will be checked then in textbox the text will be 1,9 and if checkbox1 is unchecked then the textbox text will be 9
How to do this using javascript, vb.net or Jquery
I dont have any idea how to do it ...
CODE I FOUND :
<script type="text/javascript">
function Test(control, value) {
var str = '';
$('#<%=txtTest.ClientID %> :checked').each(function() {
str = str + ', ' $(this).val();
});
$('#<%=txtTest.ClientID %>').val(str);
</script>
ITS NOT WORKING ...

Something like this, you need to give the checkboxes a class of checkBoxClass. The following has NOT been tested!
$(function() {
$(".checkBoxClass").each("click", function() { // whenever a checkbox is clicked
var str = "";
/* get all checked checkboxes of this name - class can be used too,
but using name, you can have sets of checkboxes */
$("input[name=$(this).attr('name')] :checked").each(function() {
str += ", " + $(this).val(); // append the value
});
// add it to the textfield, if there is something to add
$("#<%=txtTest.ClientID %>").val(str?str.substring(2):"");
});
});

Related

Get all input of form from a html-string with jQuery - No matter which level they are

I want to extract out of an html string all forms and their input fields. In order to do this, I've written the following code:
var html = $.parseHTML("<html><form id="Form1"><div><input name="Input1"></div></form></html>");
$(html).find('form').each(function(e) {
var FormElement = $(this);
$(FormElement).find('input, textarea').each(function(e) {
consolge.log($(this).attr('name') + " is an input field of a form with the the ID: " + $(FormElement).attr('id'));
});
});
Sadly, this extracts only the children input fields of the form. The input fields, which are in a div, which is in a form are not affected by the scanning.
(working example: html -> form -> input)
(not working example: html -> form -> div -> input)
Any suggestions?
Thanks in advance! :)
I have updated your code to use (html).each()
//with multipule forms
var html = jQuery.parseHTML("<html><form id='Form1'><div><input name='Input1'></div></form><form id='Form2'><div><input name='Input2'></div></form></html>");
jQuery(html).each(function(e) {
var id = jQuery(this).attr('id');
jQuery(this).find('input, textarea').each(function(e) {
//that will output for every input and textarea
console.log(jQuery(this).attr('name') + " is an input field of a form with the the ID: " + id );
});
});

How to get a count of checkboxes checked on pagination Jquery dataTables

Below is my code, I want to get the count of checked checkboxes of all pages in my DataTable. Please advice how to proceed
<td align="center"><input type="checkbox" align="center" id="all" class="groupCheckBox" name="emails[]" value=' . $user['id'] . '></td>
Below code I used for get the values of checked boxes in all pages on button click
$('#merge_button').click(function () {
var id = "";
var oTable = $("#userTable").dataTable();
$(".groupCheckBox:checked", oTable.fnGetNodes()).each(function () {
if (id != "") {
id = id + "," + $(this).val();
} else {
id = $(this).val();
}
document.getElementById("email").value = id;
});
});
but now I want to count the checkedboxes without button click function whenever user clicked on checkboxes and deduct count when unchecking boxes . pls advice
var table = $("#userTable").DataTable();
var countchecked = table
.rows()
.nodes()
.to$() // Convert to a jQuery object
.find('input[type="checkbox"].groupCheckBox:checked').length;
Uses the most recent API
"...please be aware that using deferRender will cause some nodes to be created only when they are required for display, so they might not be immediately available when this method is called." from https://datatables.net/reference/api/rows().nodes()
Quite easy.
$("td input[type='checkbox']:checked").length;

Variation dropdowns into radio buttons

I am trying to convert dropdown to radio buttons on the fly using jquery, here is the code
jQuery(document).ready(function($) {
$('select#pa_size option[value=' + $choice + ']').attr('selected', true).parent().trigger('change');
});
this code get me the radio button. But it cant work for add-to-cart option.
Please click add-to-cart link for see the direct page.
jQuery(document).ready(function($){
//Get Exising Select Options
$('select#pa_size').each(function(i, select){
var $select = $(select);
$select.find('option').each(function(j, option){
var $option = $(option);
// Create a radio:
var $radio = $('<input type="radio" />');
// Set name and value:
$radio.attr('name', $select.attr('name')).attr('value', $option.val());
// Set checked if the option was selected
if ($option.attr('selected')) $radio.attr('checked', 'checked');
// Insert radio before select box:
$select.before($radio);
// Insert a label:
$select.before(
$("<label />").attr('for', $select.attr('name')).text($option.text())
);
// Insert a <br />:
$select.before("<br/>");
});
$select.remove();
});
$('.single_variation_wrap').css('display','block');
});
// This is the latest code. same problem

Retrieving attribute data from selected ListBox items in code behind

I have a web application using asp.net and C#. I have a ListBox where the user can select multiple items. They are grouped using the attribute property. I need this property in the code behind in the button click event. I thought I could set the attribute values on the client side and they would be available on the server side and have learned that is not the case.
I don't know the best way to go about this. Each ListItem has a Name, Value and Group that I would like to have on the server side. The name and value are already available on the server side. I need the group associated with each selected item. Should I create a hidden field for each selected item? Should there be one hidden field with the grouping and value associated with each grouping? I have a jquery function that sets the grouping attribute. I would like to use that to set the hidden field but I am not sure if I should use one hidden field or as many as items selected.
This is the javascript that I have already:
$(document).ready(function () {
//Create groups for recipient dropdown list
$(".chosen-select option[grouping='GlobalGroups']").wrapAll("<optgroup label='Global Groups'>");
$(".chosen-select option[grouping='PersonalGroups']").wrapAll("<optgroup label='Personal Groups'>");
$(".chosen-select option[grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");
//Configure the ListBox using the 'chosen' jquery plugin
$(".chosen-select").chosen({
search_contains: true,
no_results_text: "Sorry, no match!",
allow_single_deselect: true
});
$('.chosen-container').css('width', '600px');
//set attribute property for selected list
$(".chosen-select").chosen().change(function (evt) {
$(".chosen-select").find("option:selected").each(function () {
var label = $(this).closest('optgroup').prop('label');
if (label == "Global Groups") {
$(this).attr("grouping", "GlobalGroups");
}
else if (label == "Personal Groups") {
$(this).attr("grouping", "PersonalGroups");
}
else {
$(this).attr("grouping", "Individuals");
}
});
});
});
This is the HTML:
<asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple"
data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
</asp:ListBox>
For any with this problem...I went with a hidden field, asp:HiddenField, and adding all of the selections in a semicolon delimited string.
I parsed the string in the code behind to determine the recipients that were groups and ones that were individuals.
This was my final jquery script:
$(".chosen-select").chosen().change(function (evt) {
$("#hdnRecipientAttr").val("");
$(".chosen-select").find("option:selected").each(function () {
var label = $(this).closest('optgroup').prop('label');
var currentHdnValue = $("#hdnRecipientAttr").val();
if (label == "Individuals") {
var attrText = "Individuals-" + $(this).prop('value') + ";";
$("#hdnRecipientAttr").val(currentHdnValue + attrText);
}
else {
var attrText = "Group-" + $(this).prop('value') + ";";
$("#hdnRecipientAttr").val(currentHdnValue + attrText);
}
});
//remove ending semicolon
var hdnValue = $("#hdnRecipientAttr").val();
$("#hdnRecipientAttr").val(hdnValue.slice(0, -1));
});

How to get id from generated list in jquery

$ctr = 0;
foreach($authspons as $authspons_list){
$ctr++;
echo "<input type='checkbox' id='id_list'".$ctr."'"." name='authspons[]'"." class='list_check' value='".$authspons_list->authspons_position." ".$authspons_list->authspons_lname.",".$authspons_list->authspons_fname." ".$authspons_list->authspons_mname." ".$authspons_list->authspons_cmpny.'; '."'".">" .$authspons_list->authspons_position." ".$authspons_list->authspons_lname.",".$authspons_list->authspons_fname." ".$authspons_list->authspons_mname." ".$authspons_list->authspons_cmpny."</input> <br />";
echo "<input type='text' class='text_list' id='tbox".$ctr."'"."name='authsponsid[]' value='".$authspons_list->authspons_id."; "."'"." >";
}
I have a generated list coming from database, the checkboxes contains the personal details, while the textboxes contains the "ID" of each items in the checkboxes.
This is my jquery code:
$('#list').click(function(){
var final = '';
var final2='';
$('.list_check:checked').each(function(){
var values = $(this).val();
final += values;
var val_id = $('.text_list').val();
final2 +=val_id;
});
$("#coauth").val(final);
$("#coauthid").val(final2);
$("#coauthspons").modal("hide");
});
Getting value from checkboxes is okay. My problem is: the output from the textbox is always the last value of the number of checkboxes that is checked.
Like this:
1st checkbox = 1st textbox(value="1");
2nd checkbox = 2nd textbox(value="2");
when i check the two options, the textbox only results to:
1st checkbox = 1st textbox(value="2");
2nd checkbox = 2nd textbox(value="2");
How do I get the result of each of the expected checkbox and its textbox value?
here var val_id = $('.text_list').val();
you are giving the same value for all the elements irrespective of their index
try like this
var val_id = $(this).next().val(); //this gives value of next i.e .text_list element
$('#list').click(function(){
var final = '';
var final2='';
$('.list_check:checked').each(function(){
var raw_value = $(this).val();
var id_value = raw_value.split("||");
var values = id_value[1];
final += values;
var val_id = id_value[0];
final2 +=val_id;
});
$("#coauth").val(final);
$("#coauthid").val(final2);
//alert(final2);
$("#coauthspons").modal("hide");
});
Thanks #Bhadra for your answer. it gives me an idea but I found a better solution to my own problem. I put all the values in the checkbox then I use split function to separate the "ID" from the original checkbox values(fname,mname,lname).

Categories