Reputation: 1955
I'm new to javascript and trying to adjust an existing example as Fiddle. On toggling of the button the two stored variables should be updated with new value or old value if turning toggle off.
This is what I've tried so far;
//before toggling variables are stored as
var CountPropertySource = "current_rating";
var AreaPropertySource = "current_rating";
//After toggling variable should be stored as
var CountPropertySource = "potential_rating";
var AreaPropertySource = "potential_rating";
$(function(){
$("#tglSendValue").click(function(e){
$("#sendvalue").val($("#sendvalue").val() == "current"? CountPropertySource='potential' : CountPropertySource='current' );
alert(CountPropertySource);
});
});
Upvotes: 0
Views: 213
Reputation: 30893
Working example: https://jsfiddle.net/Twisty/kafxwd70/
Consider using an if
statement as it can execute more code.
JavaScript
$(function() {
var CountPropertySource = "current_rating";
var AreaPropertySource = "current_rating";
$("#tglSendValue").click(function(e) {
if ($("#sendvalue").val() == "current") {
$("#sendvalue").val("potential");
CountPropertySource = "potential_rating";
} else {
$("#sendvalue").val("current");
CountPropertySource = "current_rating";
}
alert(CountPropertySource);
});
});
The
if
statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the
if
statement.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
Upvotes: 1
Reputation: 1
you can try to do something like this:
var baseValue = 'current_rating';
var modifiedValue = 'potential_rating';
var CountPropertySource = baseValue;
var AreaPropertySource = baseValue;
$(function(){
$("#tglSendValue").click(function(e){
if ($("#sendvalue").val() == 'current') {
$("#sendvalue").val('potential');
CountPropertySource = modifiedValue;
} else {
$("#sendvalue").val('current');
CountPropertySource = baseValue;
}
alert(CountPropertySource);
});
});
Put it in a fiddle as well. Hopefully that helps to shed a bit of light!
Upvotes: 0