I'm trying to update a series of radio button in a sharepoint 2013 custom new form, based on a select option. My code so far:
function setTierDestinations(destination) {
var radios = $("#DeltaPlaceHolderMain").find("span[title='" + destination + "']:first-child");
console.log(radios);
$.each(radios, function(i, value) {
$(value).prop("checked",true);
});
}
document.getElementByID(ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff151_ctl00_DropDownChoice).onchange = function () {
var destination = document.getElementByID(ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff151_ctl00_DropDownChoice).value;
setTierDestinations(destination);
};
<select name="ctl00$ctl43$g_f596e023_21bc_42e0_b68b_c8a7b74467a3$ff151$ctl00$DropDownChoice" id="ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff151_ctl00_DropDownChoice" title="Destination" class="ms-RadioText">
<option selected="selected" value=""></option>
<option value="Store">Store</option>
<option value="Shed">Shed</option>
</select>
<span id="radioA"><span dir="none">
<table cellpadding="0" cellspacing="1">
<tr>
<td>
<span class="ms-RadioText" title="Store">
<input id="ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff271_ctl00_ctl00" type="radio" name="ctl00$ctl43$g_f596e023_21bc_42e0_b68b_c8a7b74467a3$ff271$ctl00$RadioButtons" value="ctl00" checked="checked" /><label for="ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff271_ctl00_ctl00">Store</label>
</span>
</td>
</tr>
<tr>
<td>
<span class="ms-RadioText" title="Shed">
<input id="ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff271_ctl00_ctl01" type="radio" name="ctl00$ctl43$g_f596e023_21bc_42e0_b68b_c8a7b74467a3$ff271$ctl00$RadioButtons" value="ctl01" /><label for="ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff271_ctl00_ctl01">Shed</label>
</span>
</td>
</tr>
</table>
</span></span>
All the content in #radioA gets duplicated several times as #radioB, #radioC, etc.
I think I'm not selecting the radios var properly. In console I'm getting an array of multiple "span.ms-RadioText" where I should be getting inputs. Is this the correct usage of first-child?
Your use of first-child isn't selecting the children of your target element. You want .children()
This should work
var radios = $("#DeltaPlaceHolderMain").find("span[title='" + destination + "']").children()[0];
Which will select the first child of the span.
The :first-child selector doesn't select the first child of the element, but the element that matches the selector that is the first child of the parent.
http://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:first-child
To reiterate and elaborate on what Zakaria said,
getElementByID is written incorrectly. It should be document.getElementById
Note the lowercase d.
Your id should be in quotes, as it is a string, and not a variable. So document.getElementById('ctl00_ctl43_g_f596e023_21bc_42e0_b68b_c8a7b74467a3_ff151_ctl00_DropDownChoice').onchange etc.
Related
I am trying to update two cascading drop down inside a table there are many answers on SO for drop downs, but I was unable to find help on cascading updates inside a Table with dynamically added rows.
Given the many rows they all have varying Id's filter #Id does'nt work. So, How do I identify which rows Dropdown triggered the change & cascade the update another Dropdown/cell in the next col of the same row?
There are 2 DropDownList (select box) inside a table row cell. To simplify this, the first is Country -> Second is state. So a user is expected to select the country and then the state.
My pseudo algorithm:
find which one was fired, and which row (unsure if its needed, should I wire in place)
then fire an ajax call... to get the values based on country
update the second drop down with value in the same table row.
Case 1 Change country and then change state.
Case 2 Just change State, but first get the Country value in the first dropdown of the same row.
I know how to get the change and value in a regular page, but how do I get those changes and update the adjacent dropdown.
$(document).on('change', 'select', function(){
var data = $(this).val();
alert(data);
});
Edit, Stephen request to show HTML
<tr>
<td>
//DropDown 1 (Imagine Country)
<span class="projectcodeid">
<select class="form-control" id="Records_1__TCode_Project_ID" name="Records[1].TCode.Project.ID"><option value=""></option>
<option value="1">Plumbing</option>
<option value="2">Modeling</option>
</select></span>
</td>
<td>
//DropDown 2 (Imagine State)
<input type="hidden" name="Records.Index" value="1">
<input class="timecodeid" name="Records[1].TCode.ID" type="hidden" value="5">
<span class="timecode timecodeDdlId"> <select class="form-control timecodeDdlId" id="Records_1__TCode_ID" name="Records[1].TCode.ID"><option value=""></option>
</select></span>
</td>
<td>
<input name="Records[1].DataRecords[0].ID" type="hidden" value="">
<input class="hours" name="Records[1].DataRecords[0].Work" type="text" value="">
</td>
<td>
<input class="bs-checkbox" name="Records[1].DeleteRow" type="checkbox" value="true"><input name="Records[1].DeleteRow" type="hidden" value="false">
</td>
</tr>
Sample image for clarification
Assuming that you can't identify dropdwns by class or anything.
Assuming that every time you change the value in a dropdown you want to update the other dropdown on the same row.
Assuming that you have only two dropdowns per row:
$('table').on('change', 'select', function() {
var $current_dropdown = $(this),
$other_dropdown = $(this).closest('tr').find('select').not(this);
/// perform any task you need with current and other dropdown
});
You need to give both your <select> elements class names and use relative selectors to select the associated element in the same row.
Assuming your html is
<table id="table"> // give this an id attribute
<tbody>
<tr>
<td><select class="country" ....> ..... </select></td>
<td><select class="state" ....> ..... </select></td>
</tr>
</tbody>
</table>
Then your script will be
$('#table').on('change', '.country', function() {
var selectedValue = $(this).val();
var row = $(this).closest('tr'); // get the row
var stateSelect = row.find('.state'); // get the other select in the same row
// make you ajax call passing the selectedValue to your controller
// in the success callback, update the options of stateSelect
$.ajax({
url: ...
data { id: selectedValue },
....
success: function(data) {
stateSelect.empty();
$.each(data, function(item, index) {
stateSelect.append($('<option></option>').val(iem.ID).text(item.Name));
}
}
});
}
Refer also better way to load 2 dropdown in mvc for details of the code for populating cascading dropdownlists (consider caching the options as per the 2nd code example to avoid repeated ajax calls)
I have a table, in that table there have two radio button, and the table data populate from database, I am using foreach loop, the radio button is repeated in a row but when i select one radio button another is deselect automatically,
Here is my code through which i want achive my task.
$(document).ready(function() {
$(document).on('click', 'input[type=radio][name=test]', function()
{
var clicked = localStorage['clicked'];
$('.' + related_class).prop('disabled', false);
$('input[type=radio][name=test]').not(':checked').each(function()
{ var other_class = $(this).val();
$('.' + other_class).prop('disabled', true);
});
});
});
<table>
<c:forEach items="${List2}" var="as">
<tr>
<td><input type="hidden" name="Id" value="${as.id}"/>${as.id}</td>
<td><input type="hidden" name="RuleName" value="${as.ruleName}"/>${as.ruleName}</td>
<td> <input type="radio" name="fixed" value="Fixed"></td>
<td> <input type="radio" name="repeats" VALUE="radio20"><select name="Repeats" style="width: 80px;" >
<c:forEach items="${listfrequency}" var="freq">
<option value="${freq.frequencyName}"/>
<c:out value="${freq.frequencyName}" />
</c:forEach></select></td>
<td>
</tr>
</c:forEach>
</table>
It is because the name of the all radio inputs are same (repeats and fixed).
You need to make them unique for each row so that by selecting one radio button other doesn't deselect automatically. Best way to make them unique is append index value of foreach loop to the name of radio buttons.
I don't know the syntax how to get index/counter value of foreach loop in your example. But it would be as follow.
<td> <input type="radio" name="fixed${indexValue}" value="Fixed"></td>
<td> <input type="radio" name="repeats${indexValue}" VALUE="radio20"><select name="Repeats" style="width: 80px;" >
<c:forEach items="${listfrequency}" var="freq">
<option value="${freq.frequencyName}"/>
<c:out value="${freq.frequencyName}" />
</c:forEach></select></td>
${indexValue} replace this with your actual index/counter variable.
First of all, thank you very much for supporting me in: Change Dropdown values based on input in textfield
Now I need to extend this feature.
Here is the code for changing the drop down menue based on a text field value (Thank you very much, Rion Williams):
https://jsfiddle.net/q5s24th2/
Now I would like to add more persons for which I can also enter the textfield value so that the drop down menu changes its content. Here I have created something to add more people:
https://jsfiddle.net/xsnLc48o/
<p id="maintable">1:
<input name="Year1" type="text" value="" />
<select name="Results1">
<option selected="selected" value="">please choose:</option>
<option value="300" data-under-18="100" data-over-18="300">300.-</option>
<option value="500" data-under-18="200" data-over-18="500">500.-</option>
<option value="1000" data-under-18="300" data-over-18="1000">1000.-</option>
<option value="1500" data-under-18="400" data-over-18="1500">1500.-</option>
<option value="2000" data-under-18="500" data-over-18="2000">2000.-</option>
<option value="2500" data-under-18="600" data-over-18="2500">2500.-</option>
</select>
<input name="Additional1" type="checkbox" title="Option" value="1" />Option
</p>
<p>
<input type="button" value="+ Add Person" id="addRows" />
</p>
Unfortunately for the added persons the drop down feature does not work (I have tried several things).
If anybody has an idea how to do it, I would be very happy.
Maybe there is a much better possibility than using the addRows/append feature.
Thank you in advance.
Best regards,
Andy
You could accomplish this by creating a function that would clone one of your existing rows and append it to content. It may be best to consider using a table to more easily organize your content :
<table id="maintable">
<tr class='person-row'>
<td class='person-number'>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</table>
And then adjusting your "add rows" function to clone the first row and then store a counter of the number of current users so that you can append that to the name attribute as expected :
// When you add a new user
$("#addRows").click(function(){
// Determine the next users number
var nextUser = $('.person-row').length + 1;
// Clone the first row
var nextUserRow = $('#maintable tr:first').clone();
// Update your attributes
$(nextUserRow).find('.person-number').text(nextUser + ':');
$(nextUserRow).find('.person-year').attr('name','Year' + nextUser).val('');
$(nextUserRow).find('.person-value').attr('name','Results' + nextUser);
$(nextUserRow).find('.person-additional').attr('name','Additional' + nextUser);
// Set the defaults for the row
$(nextUserRow).find('select option:not(:first)').each(function() {
var valueToUse = $(this).attr('data-over-18');
$(this).val(valueToUse).text(valueToUse + '.-');
});
// Append the row
$("#maintable").append(nextUserRow);
});
You can see a working example of this in action here and demonstrated below :
Using jQuery 1.9.1 & getting XML returned from a query that needs to be displayed in a web page as shown in the picture below. I had asked a similar question several days ago but was all over the place in what I was asking. Hope to ask better questions this time.
For the items in the picture, the XML input would be:
<Classrooms>
<Room Number="3">
<Machine>310</Machine>
<Machine>320</Machine>
<Machine>340</Machine>
<Machine>350</Machine>
</Room>
<Room Number="8">
<Machine>810</Machine>
<Machine>820</Machine>
<Machine>840</Machine>
</Room>
<Room Number="10">
<Machine>1010</Machine>
<Machine>1020</Machine>
</Room>
</Classrooms>
The code below is a function that is called upon a successful AJAX GET and builds the checkboxes in a table on the web page.
var $roomList = $( items );
var roomListString = jQ_xmlDocToString( $roomList );
var roomListXML = $.parseXML(roomListString);
$(roomListXML).find("Row").each(function() {
var activeRooms = $( roomListXML ).find("Row").text();
var nbrRooms = $(activeRooms).find("Room").size();
$(activeRooms).find("Room").each(function() {
var roomNo = $(this).attr("Number");
var roomchk = "Room"+roomNo;
var $tr = $("<tr />");
$tr.append('<td><input type="checkbox" name="'+roomchk+'" id="'+roomchk+'" class="checkall" /><label for="'+roomchk+'">Room '+roomNo+'</td>');
$("#mytable").append( $tr );
$(this).children().each(function() {
var machID = $(this).text();
var idname = "Room"+roomNo+"Mach"+machID;
$tr.append('<td><input type="checkbox" name="'+idname+'" id="'+idname+'" /><label for="'+idname+'">'+machID+'</td>');
$("#mytable").append( $tr );
});
});
});
When the above code is run on the data, the HTML in the table is as shown below.
<tbody>
<tr>
<td>
<input name="Room3" id="Room3" class="checkall" type="checkbox" />
<label for="Room3">Room 3</label>
</td>
<td>
<input name="Room3Mach310" id="Room3Mach310" type="checkbox" />
<label for="Room3Mach310">310</label>
</td>
<td>
<input name="Room3Mach320" id="Room3Mach320" type="checkbox" />
<label for="Room3Mach320">320</label>
</td>
<td>
<input name="Room3Mach340" id="Room3Mach340" type="checkbox" />
<label for="Room3Mach340">340</label>
</td>
<td>
<input name="Room3Mach350" id="Room3Mach350" type="checkbox" />
<label for="Room3Mach350">350</label>
</td>
</tr>
<tr>
<td>
<input name="Room8" id="Room8" class="checkall" type="checkbox" />
<label for="Room8">Room 8</label>
</td>
<td>
<input name="Room8Mach810" id="Room8Mach810" type="checkbox" />
<label for="Room8Mach810">810</label>
</td>
<td>
<input name="Room8Mach820" id="Room8Mach820" type="checkbox" />
<label for="Room8Mach820">820</label>
</td>
<td>
<input name="Room8Mach840" id="Room8Mach840" type="checkbox" />
<label for="Room8Mach840">840</label>
</td>
</tr>
<tr>
<td>
<input name="Room10" id="Room10" class="checkall" type="checkbox" />
<label for="Room10">Room 10</label>
</td>
<td>
<input name="Room10Mach1010" id="Room10Mach1010" type="checkbox" />
<label for="Room10Mach1010">1010</label>
</td>
<td>
<input name="Room10Mach1020" id="Room10Mach1020" type="checkbox" />
<label for="Room10Mach1020">1020</label>
</td>
</tr>
</tbody>
Selection of any checkbox on the page will enable a SUBMIT button on the page, which when clicked will pass the value of the boxes checked into another function. The user can select any number of the individual boxes (the ones with the number beside them), regardless of the room those items are associated with. When the user selects a Room checkbox though, ALL the individual checkboxes for that room should also be checked. The individual values are the ones I want. When the SUBMIT button is clicked, the individual values are all that will be sent to that function.
I had looked at this topic about using a checkbox class to select all.
I'm using the code below to find what's checked. I can see when I select an individual box that it gets added to the array. I can also see when I select a Room that the checkall is found, but I can't seem to make the individual checkboxes get checked once I do. I had been attempting to use the Attribute Starts With jQuery selector, but haven't been able to get it to check the boxes.
$("#mytable").click(function(e) {
var ele = $(this).find(":checkbox");
var zall = $(this).find(":checkbox.checkall:checked");
if (zall) {
var zname = $(zall).attr("name");
var selectallmachines = "input[name^='" + zname +"']:checked:enabled";
$( $(selectallmachines), "#mytable");
}
var arr = [];
$(":checkbox:checked").each(function(i) {
arr[i] = $(this).val();
});
});
I'm sure that it is something that I'm overlooking, but what am I missing? Would appreciate any suggestions or guidance on what I'm doing wrong, or if there's perhaps a better way to do what I'm doing.
Thanks!
You can do something like this
$('.checkall').change(function(){
$('input[id^='+this.id+']').prop('checked',this.checked);
});
when .checkall is clicked/changes - set checked property on all inputs with an id that starts with the current clicked elements id
FIDDLE
Though you probably should delegate since you are using ajax to get the elements - this is assuming #mytable exists on DOM Ready - or replace with an ancestor element that does
$('#mytable').on('click','.checkall',function(){
$('input[id^='+this.id+']').prop('checked',this.checked);
});
another alternative is to give the checkall id as a class to the relative checkboxes
$tr.append('<td><input type="checkbox" name="'+idname+'" id="'+idname+'" class="'+roomchk+'"/><label for="'+idname+'">'+machID+'</td>');
then you can do
$('#mytable').on('click','.checkall',function(){
$('input.'+this.id).prop('checked',this.checked);
});
FIDDLE
I've got a table that can be filtered by multiple checkboxes, as well as by a "select" dropdown. Essentially, what I want to be able to do is click on multiple checkboxes in order to find each row containing that class, so class 1 and 3, or example, and then filter it by the location.
I've gotten really close at this point, where I can select both the location (which is also a class, two letters for state abbreviations), and a single class from the checkboxes, but I want people to be able to compare multiple classes from the checkboxes.
The problem is something I understand, but can't seem to figure out how to fix. The code adds each class to the end of the "tr". This means if you apply 2 filters from the checkboxes, it finds nothing, instead of finding everything from each filter. While this behavior is perfectly functional for 1 checkbox and the Dropdown, meaning it will only give me Filter 1 values from Tennessee, I want it to be able to give me Filter 2, Filter 3, and Filter 4 at the same time, if I so choose.
Any ideas on how to alter this to make it allow multiple checkbox selections?
By the way, I'm very limited in what JavaScript/jQuery plugins I can use, which is why I'm fighting so hard to use tableSorter with this solution.
My HTML:
<form name="FilterForm" id="FilterForm" action="" method="">
<input type="checkbox" name="filterStatus" value="1" /><label for="filter_1">Filter 1</label>
<input type="checkbox" name="filterStatus" value="2" /><label for="filter_2">Filter 2</label>
<input type="checkbox" name="filterStatus" value="3" /><label for="filter_3">Filter 3</label>
<input type="checkbox" name="filterStatus" value="4" /><label for="filter_4">Filter 4</label>
<select type="dropdown" name="filterStatus" id="chooseState" class="filter">
<option value="ZZ">--Choose State--</option>
<option value="TN">Tennessee</option>
<option value="MO">Missouri</option>
<option value="NV">Nevada</option>
<option value="IA">Iowa</option>
</select><label>State</label>
</form>
<table id="StatusTable">
<tbody>
<tr class="1 TN">
<td class="Name"></td>
<td class="ID"></td>
<td class="Type"></td>
<td class="Location">City, Tennessee</td>
<td class="Class"></td>
<td class="Received"></td>
<td class="Action"></td>
<td class="Status">Active</td>
</tr>
</tbody>
</table>
JavaScript:
$("input[name='filterStatus'], select.filter").change(function () {
var classes = "";
var stateClass = ""
$("input[name='filterStatus']").each(function() {
if ($(this).is(":checked")) {
classes += "." + $(this).val();
}
});
$("select.filter").each(function() {
if ($(this).val() != 'ZZ') {
stateClass += "." + $(this).val();
}
});
if (classes == "" && stateClass == "") {
// if no filters selected, show all items
$("#StatusTable tbody tr").show();
} else {
// otherwise, hide everything...
$("#StatusTable tbody tr").hide();
// then show only the matching items
rows = $("#StatusTable tr" + classes + stateClass);
if (rows.size() > 0) {
rows.show();
}
}
});
Since you know the problem, ill skip the explication and jump to the solution.
Use the .filter() method mixed with an array.
Instead of expending a string, push the class name into an array :
var classes = [];
$("input[name='filterStatus']").each(function() {
if ($(this).is(":checked")) {
classes.push('.'+$(this).val());
}
});
Then when selecting the row, use .filter() just like that :
rows = $("#StatusTable tr" + stateClass).filter(classes.length ? classes.join(',') : '*')
Then everything work fine!
Fiddle