I would like to replace the null record with N/A when possible in JavaScript.
I have the form, where some of the questions are reliant on other questions. If, for instance, some of the questions in marked as "yes" then other questions are not required, therefore they're inactive, as per below.
Next, I've prepared the email form like shown here:
HTML Assigning the checkbox to the form action already defined
and everywhere, where I have the unanswered question (while inactive ones) the result shows null.
I would like to have N/A instead of null everywhere, where questions are to be inactive due to selection.
My initial code looks like this:
var surveyFailure = formData.get('h3g_survey_failure_reason');
and I tried something like this:
var surveyFailure = if
formData.get('h3g_survey_failure_reason') = null {
surveyFailure = "N/A"
};
but it doesn't work, as i get
Uncaught SyntaxError: Unexpected token 'if'
Is there any chance to replace the null with N/A?
Full code is available here:
https://jsfiddle.net/r5kw4f6g/
You have a syntax error, the correct code shoud be this:
var data = formData.get('h3g_survey_failure_reason');
var surveyFailure = (data === null) ? "N/A" : data;
Related
I am trying to build a Gmail database for all users in our company, I want to get these Gmails through piece of code and apply an if condition on them to see if they match or not, but I am not successful so far. I don't know if it's because the string I retrieve can't be read as an apps script syntax or it's because I am doing it wrong. here is my code:
I put all needed Gmails in 1 cell (B3) like this --->
(user == 'abc1#gmail.com') || (user == 'abc2#gmail.com')
//my code
var user = Session.getEffectiveUser();
var mailDB = SpreadsheetApp.openById('Sheet ID').getSheetByName('Sheet Name'); //mails database
var cellcondition = HeadOfficeMailDB.getRange("A3"); // cell with certain value 22
var Gmails = mailDB.getRange("B3").getValue(); //retrieve Gmails in the cell as one whole string
if((Gmails) && (cellcondition == 22))
{
var newcell = HeadOfficeMailDB.getRange("C3").setValue(4);
}
I set the Database with 2 different Gmails, but when I run the code with a third Gmail not included in Database, it runs anyway. it seems like it doesn't recognize the string as a syntax, or am I doing something wrong?
kindly if you have any fix or recommendations or other better ideas to handle such issue please don't hesitate to provide me with your assistance immediately.
The following line
var cellcondition = HeadOfficeMailDB.getRange("A3");
assigns a Class Range objec to cellcondition. Replace it by
var cellcondition = HeadOfficeMailDB.getRange("A3").getValue();
to assign the value of A3 to cellcondition.
To evaluate the value of Sheet Name!B3 ((user == 'abc1#gmail.com') || (user == 'abc2#gmail.com')) you could use eval() but doing this is an enormous security risk. It's better to store the email address as a list (separated by using a separator like a comma) then use String.prototype.split() and Array.prototype.index() or create a Set object.
Related
javascript pass eval variables
I have a Jquery object like the example below
[li#myid.item.fr-hide, prevObject: _.fn.init[1], context: a.link.pjaxload]0: li#myid.item.fr-hidecontext: a.link.pjaxloadlength: 1prevObject: _.fn.init[1]__proto__: _[0]
The object has an arrow pointing down and when i click the arrow , there are some other variable arrows namely 0: li#myid.item.fr-hide,accesskey etc. And when i click the 0, there are some more collections and among which is the baseUri.The baseUri contains a string uri with the part of the string which is myid.
I want to compare and check that object all the time if part of that baseUri example
//www.mydomain.com/myid?l=FR
contains myid . I tried everything possible but because my object is not a string i just count get it going/comparing
i tried.
if(/myid/i.test($active.item.get(0).outerHTML){
}
It failed.
I tried
if(/myid/i.test($active.item.id.outerHTML){
}
It failed. I tried many more that i lost count. Note myid is an element that has id and its id is what i am after. But the id is also contain in the baseUri. Please any help would be appreciated.
I finally get it going. active.item is a jQuery object. Below is what works for me
var $elementid = active.item.get(0).id;
if(active.item) {
if(page.language() === 'FR' && /myid/i.test($elementid)){
//My code
}
}
Another issue i encounter was var $elementid = active.item.get(0).id; passed my element id value to $elementid. However
if(page.language() === 'FR' && /myid/i.test($active.item.get(0).id)){
//My code
}
The above failed and always return false. When i log active.item.get(0).id on the console, it turns out it returns an object not my id . This i am not sure though , looking for a better explanation as well.
i've spent ages trying to understand bootstrap's navigation bar, mainly by spending 4-5 days reading stackoverflow posts
& finally i think i've found an answer that helps!!!
trouble is, i can't understand the accompanying javascript/jquery code. i'm guessing its a shorthand version of js or something but just what it means i cannot decipher
basically, its the javascript code that appears on this jsfiddle page
$('.navbar').on('show', function () {
var actives = $(this).find('.collapse.in'),
hasData;
if (actives && actives.length) {
hasData = actives.data('collapse')
if (hasData && hasData.transitioning) return
actives.collapse('hide')
hasData || actives.data('collapse', null)
}
});
so, if anyone can explain to me what the code is doing on a line by line basis it'd be really cool
the first line i understand. its the weird-ass syntax in the next 6 lines that have me mystified
var actives = $(this).find('.collapse.in'),
hasData;
This creates two variables. One with elements picked from current scope that match the selector .collapse.in, and one empty variable.
if (actives && actives.length)
If actives exists and contains more than zero elements, do the following...
hasData = actives.data('collapse')
Retrieve arbitrary data stored under the key collapse. See https://api.jquery.com/jquery.data/ for more info.
if (hasData && hasData.transitioning) return
If hasData exists and hasData.transitioning is truthy, stop function execution.
actives.collapse('hide')
Call the collapse function on actives. This is not a native jQuery function, so you'll have to look up whatever plugin it comes from to make sense of the argument being passed in.
hasData || actives.data('collapse', null)
If hasData is truthy, skip this line. Otherwise, set the arbitrary data in actives variable to null.
im trying to remove all text except one in javascript
the idea is do something like
//retrieve something like "cnode_72 cnode_1 layout_1 ui-datepicker-week-end "
// "ui-datepicker-week-end" is not necessary always present
var classes= jQuery("someelement").attr("class");
classes = classes.replace(/(?!ui-datepicker-week-end)/,'');
the expected ouput is :
ui-datepicker-week-end
Javascript (as per question)
Erm... might be missing something here, but how about:
classes = "ui-datepicker-week-end";
If that text isn't in the original string, and thus you don't want to add it if it isn't already there then try this:
if(classes.indexOf("ui-datepicker-week-end") == -1)
classes = "";
else
classes = "ui-datepicker-week-end";
JQuery (as per request in comments)
If by some bizarre reason you are using the jQuery("...").attr("class") but didn't bother mentioning that, then try this:
jQuery("...").removeAttr("class").addClass("ui-datepicker-week-end");
or with the condition:
if(jQuery("...").hasClass("ui-datepicker-week-end"))
jQuery("...").removeAttr("class").addClass("ui-datepicker-week-end");
else
jQuery("...").removeAttr("class");
Here is a working example
Update: clarified question (I hope)
Hi.
I'm developing a plugin in Wordpress and I'm outputting elements according to user privileges A and B.
In case of A, I ouput element "Foo".
In case of B, I output element "Bar".
Up till now, I haven't checked if an element exists before I try to retrieve the value.
This of course gives me a javascript error in some browsers (like IE7).
I've looked at using the typeof() function:
if(typeof(element) == 'undefined') {
//do something...
}
I'm also using jQuery. So one solution could be using this:
if ($("#mydiv").length > 0){
// do something here
}
Using the above methods, makes me having to check each element before trying to retrieve any values.
The "ideal" solution would be to get values based on user privileges. E.g:
if (userPriv == A) {
//get values from element 'Foo'
}
This way I can check once, and do the data gathering. The only solutions I can think of are setting the value of a hidden input element or use cookies.
<input type="hidden" id="userPriv" value="A" />
The other solution would be adding a value to the cookie.
setcookie("userPriv", "A");
Unfortunately, this last option gives me a warning message saying that cookie must be set in header (before html output). I think it's because I'm doing this in Wordpress.
I'm looking for opinions on which method is "the best way" to accomplis this.
Forgive me if I'm missing something, but checking for a DOM element in javascript is usually pretty easy.
var elementA = document.getElementById('id_of_a');
var elementB = document.getElementById('id_of_b');
if (elementA) {
//...
} else if (elementB) {
//...
}
The key is the if statement. getElementById will return nothing null if the element is not found, which will evaluate to false in the if statement.
Alternatively, if you don't really want to check for existence of individual DOM elements, can you send the users priv in a hidden input and act on that? That's a cookie free way of sending values clientside. Something like (edited to have jQuery code instead)
<input type="hidden" id="userPriv" value="A" />
...
var priv = $('#userPriv').val();
if (priv == 'A') {
//...
}
I'd still recommend checking for individual elements over checking a hidden input. It seems cleaner to me, more along the unobtrusive lines
You can use object as associative array:
var map = new Object();
map[A.toString()] = new Foo();
map[B.toString()] = new Bar();
In that case is much simpler to check and you will avoid "spaghetti code".