Im trying to populate and show a select if the previous 12 dropdowns have a value.
I cant seem to get my if statement correct though, can anybody see what I may be doing wrong?
$('select').on('change',function(){
if( $('.player1') != null || $('.player2') != null ){
// If both players have been selected, show the winner dropdown and populate it with the value from player1 and player 2.
$('.winner').show();
}
});
http://jsfiddle.net/kbPLn/1/
A jQuery object will never equal null. What you can do is check the length property of the collection. Also, it looks like you want && instead of ||.
if ($('.player1>option:selected').length && $('.player2>option:selected').length) {
EDIT: What you rather need to do is check if it has a selected child option.
the cases you have will never be null.. (as youre not checking .val...and also, they always have a val) updated your HTML + your selectors here:
http://jsfiddle.net/kbPLn/5/
if( $('.player1>option:selected').val() != "No value" && $('.player2>option:selected').val() != "No value" ){
updated
http://jsfiddle.net/kbPLn/9/
var first = $('.player1>option:selected').val(),
second =$('.player2>option:selected').val();
$('.winner').find('option:first').val(first).html(first);
$('.winner').find('option:not(:first)').val(second).html(second);
$('.winner').show();
you need to use && when checking negation values
Try this. This may help you:
$('select').on('change', function () {
$('select[name="winner"]').html('');
if ($('.player1') != null || $('.player2') != null) {
// If both players have been selected, show the winner dropdown and populate it with the value from player1 and player 2.
$('.winner').show();
//$('.winner').
}
$('select[name="winner"]').append('<option value="' + $(".player1").val() + '">' + $(".player1").val() + '</option>');
$('select[name="winner"]').append('<option value="' + $(".player2").val() + '">' + $(".player2").val() + '</option>');
});
Related
I have 2 fields that I would like to check if their values are equal with "current_outstanding_balance" If so, it adds text to the field "current_outstanding_balance_check" . It's working when i add one var to loop. But i tried add more like, but it's not working.
if(overduer && overdue == fieldNameForResult)
This is my code:
function(){
var fieldNameForResult = thisPointer.entity.getValue('current_outstanding_balance');
var overduer = thisPointer.entity.getValue('additional_personnel_costs');
var overdue = thisPointer.entity.getValue('overdue');
if(overduer && overdue == fieldNameForResult){
jQ('[id="current_outstanding_balance_check"]').val('True' );
}else{
jQ('[id="current_outstanding_balance_check"]').val('False' );
}
} ```
Now I have no error, but it shows me "False" even though both fields are empty.
You need to add the check for both values:
if(parseFloat(overduer) + parseFloat(overdue) == parseFloat(fieldNameForResult)){
I have the below code in javascript, here location id is a text box in my UI:
var locationIdValue = $('[name="location_id"]').val();
var chainIdSelection = $('[name="chain_id"]').val();
if (chainIdSelection == "" && locationIdValue == "") {
this.displayFailureMessage("Chain # is required.", 10000);
canSearch = false;
}
The Chain Id is a drop-down for which I have the below code:
<select class="${prefix}ChainIdSelect" name="chain_id">
<option value=''><fmt:message key="select"/></option>
<c:forEach var="chainId" items="${ chainDescriptionList }" >
<option value="${chainId.chainId}"> ${chainId.chainId} - ${ chainId.description }</option>
</c:forEach>
</select>
chain Id declaration is:
{mData :"chain_id"
,sTitle :'<fmt:message key="location.summary.chain_id"/>'
,sClass :"chainId"
,bSortable:true
,"filter" :{selector:".${prefix}ChainIdSelect"}
}
I am trying to disable chain id drop down when my location id text box has some value. I tried the below solution:
if(locationIdValue != "" || locationIdValue != null){
document.getElementByName("chainid").disabled = true;
}
This has not worked, and so have many other things which I have tried so far. Can anyone suggest a solution please.
Not sure exactly about your code but probably you want to implement disabling logic in event callback. I want to say add callback which will be triggered every time when location_id is changed.
In your case it would be something like this:
$('[name="location_id"]').change(function(){
if(locationIdValue != "" || locationIdValue != null){
$('[name="location_id"]').prop("disabled", true);
}
});
Please verify that callback is executed when value is changed!
I have a number of the following drop-down lists within this page, each with a list of selectable users. When a user is selected in one list, I don't want them to be selected in any further lists. For this reason I have written a script to disable them.
When the form is submitted, the username of each user selected in a drop-down list should be appended to the Uservalue in the Form Collection.
This works correctly without the disabling of names but when I add seat-selectinto the class description, the names are always returned in the FormCollection as empty strings.
Do you know why this is or what I can do to keep the names populating correctly in the FormCollection?
Drop-Down List:
#Html.DropDownListFor(m => m.User, Model.UserList, "Select User", new { #class = "form-control seat-select", #id = "uniqueID", #onchange = "cleanUsers(this);" })
Java Script:
function cleanUsers(ddl) {
var val = ddl.options[ddl.selectedIndex].value;
var vOldVal = $("#" + ddl.id).attr("data-selected");
$("#" + ddl.id).attr("data-selected", val);
//Remove selected
if (val != 0) { $(".seat-select option[value='" + val + "']").attr("disabled", true); }
if (vOldVal != undefined) { $(".seat-select option[value='" + vOldVal + "']").attr("disabled", false); }
}
UPDATE 06/06/2016:
Apologies to bring back an old post. I have tried the advice listed below and this works to return one of the selected values. Unfortunately, In my solution I have a number of identical drop-down lists and I need to keep track of exactly which result is from which drop-down.
I have posted my new code below. The string is overwritten when each of the drop-down menus is changed so does not currently work for me.
New JavaScript:
function cleanRowers(ddl) {
var val = ddl.options[ddl.selectedIndex].value;
var vOldVal = $("#" + ddl.id).attr("data-selected");
$("#" + ddl.id).attr("data-selected", val);
//Remove selected
if (val != 0) {
$("#SelectedUserText").val(val);
$(".seat-select option[value='" + val + "']").attr("disabled", true);
}
if (vOldVal != undefined) { $(".seat-select option[value='" + vOldVal + "']").attr("disabled", false); }
}
The Hidden value being updated:
#Html.HiddenFor(m => m.SelectedUserText)
I am trying to make the code as re-usable as possible so dont really want a different identifier for each of the drop-downs but it is important that I keep track of which drop-down the information is coming from.
Is there a way in which this can be done?
Disabled attribute do not put value in form submission thats why you are receiving empty string .
Here is a trick you can apply :
Put the selected value in a hidden input field onchnage function then read the hidden value on form submission instead of reading vslue of disabled dropdown.
Hope that helps .
I have a button where i append inputs to the HTML DOM.
Later on i have a button to fetch input values if they matches with a keyword.
In this example "a".
HTML
<button class="btn btn-info" id="btnAddInput">Add input</button>
<button class="btn btn-info" id="fetchValue">Fetch value</button>
<div id="inputs"></div>
JS
$('#btnAddInput').on('click', function() {
$('#inputs').append('<input type="text" class="myInput"><br>');
});
$('#fetchValue').on('click', function() {
var value = $(document).find('input[value="a"]');
console.log(value);
});
Fiddle: https://jsfiddle.net/Ljrkdm53/
I´ve learned that, if you add HTML to the DOM with Jquery, you sometimes have to use document as selector, to find elements.
But i have no success in this case.
Inputs that you add is, in my code saved into mysql.
And if you load up all saved inputs at start, the js code find values.
So, what am i missing?
You're confusing the various values associated with inputs. You're not the only one!
The value attribute specifies the initial value of the input. It does not change when the input's value changes, and so since you're appending an input that has no value attribute, then typing in it, it doesn't suddenly get a value attribute — so you can't search for it by that value.
The value property on HTMLInputElement instances reflects the input's current value.
There's also the defaultValue property, which reflects the value attribute.
If you need to find an input based on its current value, there's no CSS selector that will do it, you need to use a broader search and filter:
var inputsWithA = $("input").filter(function() {
return this.value == "a";
});
Here's a quick example showing the values of an input's value property, defaultValue property, and value attribute:
$("button").on("click", function() {
var input = $("input");
msg("The input's <code>value</code> property is: '" + input.val() + "'");
msg("The input's <code>defaultValue</code> property is: '" + input.prop("defaultValue") + "'");
msg("The input's <code>value</code> <strong>attribute</strong> is: '" + input.attr("value") + "'");
msg("We can only use CSS with the attribute, so for instance <code>$('input[value=\"original\"]')</code> will find it but <code>$('input[value=\"" + input.val() + "\"]')</code> will not:");
msg("<code>$('input[value=\"original\"]')</code> found it? " +
($('input[value="original"]').length ? "Yes" : "No")
);
msg("<code>$('input[value=\"" + input.val() + "\"]')</code> found it? " +
($('input[value="' + input.val() + '"]').length ? "Yes" : "No")
);
});
function msg(html) {
$("<p>").html(html).appendTo(document.body);
}
<p>Type something in the input, then click the button:</p>
<input type="text" value="original">
<button type="button">Click Me</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
If I run that and change the input's value to "updated" before clicking the button, I get:
The input's value property is: 'updated'
The input's defaultValue property is: 'original'
The input's value attribute is: 'original'
We can only use CSS with the attribute, so for instance $('input[value="original"]') will find it but $('input[value="updated"]') will not:
$('input[value="original"]') found it? Yes
$('input[value="updated"]') found it? No
Here is the code you need.
$('#btnAddInput').on('click', function() {
$('#inputs').append('<input type="text" class="myInput"><br>');
});
$('#fetchValue').on('click', function() {
var value = $('.myInput').val();
console.log(value);
});
You can check it working here:
jsfiddle.net/Ljrkdm53/7
What you are missing is that the find returns an array of objects and not one value and that the value selector only uses the initial value. You need to use an each function on the value you have now to do something with it.
$(document).find('input').each(function () {
if( $(this).val() == "a")
console.log( $(this).val());
});
Try with each function.
$('input').each(function() {
if ($(this).val() == 'a') {
console.log('a');
}
});
I'm trying to allow users to add a list of 'favourites' to a text box but when adding more than one value it replaces the value already there. Can anybody help? Thanks this is my code:
var name
function getFavourite() {
name = "Student 1, ";
$('#output').val(name)
saveFavourites();
}
function getFavourite2() {
name = "Student 2, ";
$('#output').val(name)
saveFavourites();
}
function saveFavourites() {
var fav = $("#output").val();
if (fav !== "") {
localStorage[name] = $("#output").val();
$("#output").val(name);
}
}
function loadFavourites() {
var fav = $("#name").val();
if (name !== "") {
$("#output").val(localStorage[name]);
$("#name").val("");
}
}
using val will replace the existing value as you already noticed so i would do something like this if you want to add to that value.
$("#output").val($("#output").val() + ', ' + name);
At least if i understand you correctly. This would get the excising value and then add the new value to it (in this case with a comma but is not necessary)
Of course if you need the same element twice or more is better to assign it to a var instead of calling the selector twice.
I think you are looking into multiple select dropdown, something like this:
http://codepen.io/martynasb/pen/kawxq
You don't an text input, you want a select where you can select multiple values. In html, it's <select multiple>.
The plugin I know that provides the best experience for this is is Select2: http://ivaynberg.github.io/select2/#basics
And you don't have to load all the options right away, they can be fetched via ajax easily.