Script doesn't work with "&"-symbol - javascript

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

Related

onClick JavaScript Button

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 )}')
}

HTML to XML function issue

I currently have a function in jQuery where It will generate the HTML input fields into XML tags, But there is an issue am having at the moment, I am outputting what I put into the input fields which is great but I keep getting Undefined coming up followed by what I had entered and I cant figure out what it is, something minor but am not too sure.
Below is the function doing all the good stuff, ill link a fiddle so it offers a better insight into whats happening.
$("#form").on("submit", function(event) {
console.log('test');
$("#form").children().find("input").each(function() {
if ($(this).attr("type") == "text") {
output += $(this).attr("data-value");
output += $(this).val();
} else if ($(this).prop("tagName") == "BR" || $(this).prop("tagName") == "FORM") {
output += "
";
} else if ($(this).attr("type") == "submit") {
console.log("ignored");
} else {
output += $(this).attr("data-value");
}
});
$("#output").append(output);
Its a minor issue but I just cant see what it is, advice or pointed in the right direction would be great.
Fiddle Link: Fiddle

Handling Multiple IDs In A Single jQuery Statement

I've tried searching the site, but am really struggling to find what I want... Basically I have some jQuery code that checks the state of three IDs, they are tied to three checkboxes;
$('#submitButton').click(function(){
if($("#cb1,#cb2,#cb3").is(':checked'))
return true;
else
return false;
});
How would I restructure this jQuery statement to make it so that all three checkboxes have to be CHECKED? At the moment, either one can be checked for the action to be performed
I'm betting there is a really simple solution to all of this, but I have been lpooking at it for so long, I just can't see it. Could someone with a fresh pair of eyes and a less addled brain please steer me in the right direction?
You need to use:
$('#submitButton').click(function(){
if ($('#cb1').is(':checked') && $('#cb1').is(':checked') && $('#cb3').is(':checked')) {
return true;
} else {
return false;
}
});
Use
if($("#cb1").is(':checked') && $("#cb2").is(':checked') && $("#cb3").is(':checked'))
$("#cb1,#cb2,#cb3").is(':checked') will return result for the 1st element only
Try:
$('#submitButton').click(function(){
if($("#cb1:checked,#cb2:checked,#cb3:checked").length === 3)
return true;
else
return false;
});
You can use:
if ($('#cb1:checked,#cb2:checked,#cb3:checked').length == 3) {
//all three are checked.do something
}
or
if ($('#cb1:checked,#cb2:checked,#cb3:checked').length == $('#cb1,#cb2,#cb3').length) {
//all three are checked.do something
}
You could try:
$('#submitButton').click(function(){
if($('#cb1')[0].checked && $('#cb2')[0].checked && $('#cb3')[0].checked)return true;
return false;
});
You can just add a class to your checkboxes and use like so:
$('#submitButton').click(function(){
if($(".myCheckbox:checked").length == 3) {
console.log('true');
}
else {
console.log('false');
}
});
Here is the JSFiddle Example:
http://jsfiddle.net/cLH8s/1/

jquery .show not running

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");
}

How to check if a Textarea is empty in Javascript or jQuery?

How do I check if a textarea contains nothing?
I tried with this code:
if(document.getElementById("field").value ==null)
{
alert("debug");
document.getElementById("field").style.display ="none";
}
But it doesn't do what I expect.
I expect that it should appear a messagebox "debug" and that the textarea is not shown.
How can I fix that issue?
You wanna check if the value is == "", not NULL.
if(document.getElementById("field").value == '')
{
alert("debug");
document.getElementById("field").style.display ="none";
}
UPDATE
A working example
And another one using TRIM in case you wanna make sure they don't post spaces
Implementation for TRIM()
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
You can use following jQuery to escape white spaces as well.
if($("#YourTextAreaID").val().trim().length < 1)
{
alert("Please Enter Text...");
return;
}
There is a world of difference between null and empty !
Try this instead:
if(document.getElementById("field").value == "")
{
alert("debug");
document.getElementById("field").style.display ="none";
}

Categories