onClick JavaScript Button - javascript

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

Related

TypeError .val() is not a function on my selected answer on Javascript

I'm trying to check if the answer selected is correct, if so, then it should add + 1 to my user score.
But I don't understand why my code doesn't work. Could you please help me where the bug or did I miss something else?
function loadNextQuestion () {
var selectOption = document.querySelector('input[type=radio]:checked');
if (selectOption.val() == questions[answer]) {
++userScore;
} else if (selectOption) {
++currentQuestionNum;
loadQuestion();
loadQuestionNum();
} else {
alert("Please pick an answer.");
}
};
$("#nextButton").click(loadNextQuestion);
});
Your radio button element is not in a jQuery wrapper object, so the jQuery method val() won't work.
Try
if(selectOption.value == questions[answer]) {
...
}
instead.
The reason your code is not working is because you're comparing a Boolean (selectOption.val() [which is wrong anyway - it should be selectOption.value]) to an element of an array (questions[answer]), and then in your else if statement you're asking if selectOption is true. This code will always run.
Fixing your code only requires you to change this:
if (selectOption.val() == questions[answer]) {...}
Into something like this:
if (selectOption.attr("id") == questions[answer]) {...}
With the HTML like this:
<input type="radio" id="Chocolate-ice-cream" />
And your questions array like this:
var questions = ["Chocolate is used in what flavour of ice cream", "Chocolate-ice-cream", ...];
Hopefully what I've said makes sense, but please tell me if you don't understand anything.

Javascript - onclick function change boolean value and change attr visibility

So I'm learning Javascript and I have a doubt on changing a global variable with boolean variable, while changing the attr of visibility on an element.
The code is this:
var lastView=false;
$("#idShipmentActionsCombo-icon").on('click', function(){
if (lastview=false){
$('#idShipmentActionsCombo-lb').attr('style', 'visibility: visible');
lastView=true;
}
else if(lastView=true){
$('#idShipmentActionsCombo-lb').attr('style', 'visibility: hidden');
lastView===false;
}
}
So #idShipmentActionsCombo-icon is the element I click in, #idShipmentActionsCombo-lb and this is what I want to show and hide depending on the value of lastView.
Thanks in advance, and I apologize for my English since it's not my main language.
Since you use jQuery use .toggle() method instead of booleans, conditions and style.
$("#idShipmentActionsCombo-icon").on('click', function(){
$('#idShipmentActionsCombo-lb').toggle();
})
Looks like you're missing a closing ); at the very end from your .on( In addition, there are a few cases where "===" and "=" are confused and where capitalization is incorrect. See this: http://jsfiddle.net/215sxj90/3/
In my opinion you're confusing assignment with logical operators.
The following is the assignment:
lastView = true;
and the following is the logical operator - comparison:
lastView === true
The latter should be used in your conditional statements - if, else if etc.:
var lastView = false;
$("#idShipmentActionsCombo-icon").on('click', function () {
if (lastview === false) {
$('#idShipmentActionsCombo-lb').attr('style', 'visibility: visible');
lastView = true;
}
else if (lastView === true) {
$('#idShipmentActionsCombo-lb').attr('style', 'visibility: hidden');
lastView = false;
}
}

How to check if one element is exist under a form in jQuery

I have a form with id commentform and if any logged in user visit the page a p tag gets generated under the form that with class logged-in-as. Now I am trying to check if that p exists and if not exists then do my validation which uses keyup(). Here is a small snippet...
$('form#commentform').keyup(function() {
if( ! $(this).has('p').hasClass('logged-in-as') ) {
....
} else {
......
}
}
});
Now the problem is that the if( ! $(this).has('p').hasClass('logged-in-as') ) is not returning me the expected result whether or not that specific p exists.
Can any of you guys tell me any other/better way to check this?
$('form#commentform').keyup(function() {
if($(this).find('p.logged-in-as').length == 1) {
....
} else {
......
}
}
});
You can do this to find it.
You can use
if ($('.logged-in-as', this).length)) {
But I would rather use a variable to store that state instead of relying on checking the presence of a raw tag : what if you change your HTML a little ?
Side note: Don't use overqualified selectors. $('#commentform') is faster and logically more consistent than $('form#commentform').
Check if an element witth class "xxx" exist
if( $( ".xxx" ).size() > 0 ) {
// EXISTS
}
Edit: forgot the dot ( ".xxx" )

Script doesn't work with "&"-symbol

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

comparing innerHTML value with a string variable in javascript

function Open() {
var cc = document.getElementById('FName');
if ('Newfile.rtf' == cc.innerHTML)
{
alert("New File");
} //close If NewFile.rtf
else {
alert("Not new file");
}
}//close Open()
Here I have string "NewFile.rtf" in a element with id="FName" on the page. When the FName contains "Newfile.rtf" in it it stills goes to the else part of the function instead of going to if part. I tried different ways to write the compare statement in the if condition, no luck . Appreciate the help if anyone can help figure out this.
Thank you.
The simplest explanation is that your cc.innerHTML call is not returning what you think it is returning. Why don't you console.log or debug.
add something like
var innerhtml = cc.innerHTML;
console.log("innerHTML = " + innerhtml) // wont work in IE.
before the if statement.
Try using regular expressions to find your filename, also check if the text you are searching is not into another DOM element, elimate left and right spaces, you should use Google Chrome for debuging the Javascript code:
var html = document.getElementById('FName').innerHTML;
if( html.search("Newfile.rtf") != -1) { /*found*/ }
else { /*not found*/ }
but what's the type of this element? if it's about an input text type .. you can't use innerHTML but you'll use value then.
Use innerText to get that
function Open() {
var cc = document.getElementById('FName');
if ('Newfile.rtf' == cc.innerText)
{
alert("New File");
} //close If NewFile.rtf
else {
//enter code here
alert("Not new file");
}
}

Categories