The following code doesn't seem to execute properly for me, and I'm puzzled as to why. NonITView and ITView are both divs in my ASP page. txtIsIT is an asp:textbox on the page that gets either "yes" or "no" on page load.
if (document.getElementById("<%= txtIsIT.ClientID %>").value = "yes") {
$("#NonITView").hide("slow");
$("#ITView").show("slow");
}
else if (document.getElementById("<%= txtIsIT.ClientID %>").value = "no") {
$("#ITView").hide("slow");
$("#NonITView").show("slow");
}
else {
alert("Error");
}
The if is evaluating properly. In firefox's web console, by entering the jquery .show/.hide functions, the divs are properly shown/hidden, which is part of what's confusing me. Anything sticking out to you that should be fixed?
bug at the = on both lines, you make them equal, you not check them.
document.getElementById("<%= txtIsIT.ClientID %>").value = "yes"
a Tip, to avoid this kind of bugs write them reverse, as
if( "yes" == document.getElementById("<%= txtIsIT.ClientID %>").value) {}
that way, if by accident write = it will throw an error.
In the if statement, you're doing an assignment.
document.getElementById("<%= txtIsIT.ClientID %>").value = "yes"
This will always evaluated to true. So change that line to be:
if(document.getElementById("<%= txtIsIT.ClientID %>").value === "yes")
Also note the three equals signs. It is common practice now for equality to use ===. See this article on why:
Difference between == and === in JavaScript
You can use something like this...
$(document).ready(function() {
$('#Button_ID').click(function() {
if ($("#<%= txtIsIT.ClientID %>").val() == "yes") {
$("#NonITView").hide("slow");
$("#ITView").show("slow");
}
else if ($("#<%= txtIsIT.ClientID %>").val() == "no") {
$("#ITView").hide("slow");
$("#NonITView").show("slow");
}
else {
alert("Error");
}
});
});
(document.getElementById("<%= txtIsIT.ClientID %>").value = "yes")
should be
(document.getElementById("<%= txtIsIT.ClientID %>").value == "yes")
on if statements it should be ==
Found my problem. When an UpdatePanel reloads, it restores the attributes of it's contents to their default state, which in this case was style="display:none" on both divs. The code would run, show the appropriate div, then immediately hide it again, because later on in the function I was telling it to fire the UpdatePanel's postback trigger.
Thanks for your help everyone!
Why are you mixing jQuery and regular javascript. If you are going to use jQuery stick with it:
if ($("#<%= txtIsIT.ClientID %>").val() == "yes") {
$("#NonITView").hide("slow");
$("#ITView").show("slow");
}
else if ($("#<%= txtIsIT.ClientID %>").val() == "no") {
$("#ITView").hide("slow");
$("#NonITView").show("slow");
}
else {
alert("Error");
}
Related
I am kinda new person in JavaScript, please help me our here with small doubt
What I am missing here ........ I want to correct this one....
I am trying this :
if({!Account.CID__c}){
window.open( '{! SUBSTITUTE($Setup.CustomSetting__c.Link__c,"[#CID#]",Account.CID__c)}');
}
else{
window.open( '{! SUBSTITUTE($Setup.CustomSetting__c.Link__c,"[#CID#]",Account.AnotherField__C )}')
}
AND GOT ERROR :
Now its giving me "Unexpected token )" error
Earlier I was having
window.open( '{!SUBSTITUTE($Setup.CustomSettings__c.Link__c,"[#CID#]",Account.CID__c)}');
And I just want to add one more Condition here i.e.
If(CID__C == null){
// put another AnotherField__C in place of that.
}else{
//put Account.CID__C ...which we were having earlier
}
In javascript null is falsey. The ! operator before the statement will invert the comparison (ie, null will become true). Therefore you could do just do something like this. Also, note how I used brackets.
If (!Account.CID__c) {
//do something
} else {
//do something
}
You can also use 'else if' statements before the final else to nest additional parameters.
This worked for me .... thanks to #Ashwani
if( '{!Account.CID__c}' == 'null' || '{!Account.CID__c}' == ''){
window.open( '{! SUBSTITUTE($Setup.CustomSetting__c.Link__c,"[#CID#]",Account.AnotherField__C)}');
}
else {
window.open( '{! SUBSTITUTE($Setup.CustomSetting__c.Link__c,"[#CID#]",Account.CID__c )}')
}
Here is my Code:
$(document).ready(function () {
$("input[type='checkbox'][name='mycheckboxname']").change(function () {
if ($(this).checked == true) {
//do something (script 1)
}
else {
//do something else (script 2)
}
})
});
What i want to accomplish is to have 2 scripts running depening whenever user checks or unchecks a "mycheckboxname" checkbox.
Problem is that with above code i get always only script 2 to run so it looks like if $(this).checked is always false even if user checks the checkbox. Am I using $(this) the wrong way?
checked is a DOM property of the element, but $(this) returns a jQuery object.
You could just use the DOM property:
if (this.checked)
There's no reason to wrap this in a jQuery instance for that. I mean, you can:
if ($(this).prop("checked"))
...but it doesn't do anything useful on top of this.checked.
Side note:
if (someBoolean == true)
is just a roundabout way of writing
if (someBoolean)
I mean, why stop there? Why not
if ((someBoolean == true) == true)
or
if (((someBoolean == true) == true) == true)
or... ;-)
Use $(this).prop("checked") to get the true/false value of a <input type="checkbox">
Click the run code snippet below to see it work
$("input[type=checkbox]").change(function(event) {
if ($(this).prop("checked") === true) {
alert("ON");
}
else {
alert("OFF");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox">click it
Noticed that everything in my if-statements with an "&"-symbol doesn't work. How do I fix this?
if(id == 'EX&219'){
//do someting
}
This doesn't work. So happy for any help :)
Working Perfect
var id = 'EX&219';
if(id == 'EX&219'){
alert("equals");
} else {
alert("not equal");
}
Reference
What do you mean "everything in my if-statements with an "&"-symbol doesn't work" ?
Try this
if(id == 'EX'+String.fromCharCode(38)+'219'){
//do someting
}
38 is the char code for '&' character, if your don't want the '&' to appear in your statment.
http://www.w3schools.com/jsref/jsref_fromCharCode.asp
I am attempting to include an If statement in some Javascript on my .aspx page.
I declare FinishedPacking at the beginning of the page as False. Then when a user clicks the orderSubmit button, the if Statement evaluates if the value is indeed false, if so, display an alert. So far the if statement does not work. If I use just the alert with no if statement it displays the alert:
var FinishedPacking = false;
$("#orderSubmit").click(function (e) {
if (FinishedPacking = false) {
alert("The order is not finished.")
}
ClearScreen();
GetOrder();
}):
As stated if I do not include the if statement, the alert works when I click the order button. Not sure why this simple If statement is not being picked up.
You need the double-equals
if (FinishedPacking = false)
should be
if (FinishedPacking == false)
Try this
var FinishedPacking = false;
$("#orderSubmit").click(function (e) {
if (FinishedPacking == false) {
alert("The order is not finished.")
}
ClearScreen();
GetOrder();
}):
You need 2 ='s signs
if (FinishedPacking == false) {
You are trying to ASSIGN false to the variable FinishedPacking inside your condition (which is wrong) you want to COMPARE the values.
Hi basically i can't seem to get the form validation to work on my form in ie8 or below. It works absolutely fine every where else though.
Basically its along these lines:
function validateform() {
if (document.getElementById('firstinput').value == '') {
alert("Select start Date");
document.getElementById('firstinput').focus();
return false;
}
if (document.getElementById('secondinput').value == '') {
alert("Select end Date");
document.getElementById('secondinput').focus();
return false;
}
if (document.getElementById('restriction').checked == false) {
alert('Please select I have read and understand the above restrictions.');
return false;
}
if (document.getElementById('termscondition').checked == false) {
alert('Please select terms and condition..');
return false;
}
if (document.getElementById('letters_code').value == '') {
alert("Enter captcha code..");
document.getElementById('letters_code').focus();
return false;
}
return true;
}
Just wondered if there are any obvious mistakes that i can't see :s thanks for any help in advance.
Editing because i phased this terribly!
I have encountered an issue involving a submit button being named submit, that only failed in IE, and not in firefox/chrome, if you could make sure that is not the issue, that would help!
Alternatively, more information would be helpful, as there does not appear to be anything wrong with the code posted