How to pass a string value to firebase child( )? - javascript

It works like this:
var firebaseRef = firebase.database().ref().child(document.getElementById("areaCode").value);
Or any suggestions to pass x like this:
var areaCodex = document.getElementById("areaCode");
var x = areaCodex.toString();
firebase.initializeApp(config);
var rootRef = firebase.database().ref().child(x);
$('#saveBtn').click(function() {
rootRef.set({
date: $('#date').val(),
timeF: $('#fromTime').val()
});
})
I need to save data to Firebase and my code is as follows:
var toTime = document.getElementById("toTime");
var areaCode = document.getElementById("areaCode");
function submitClick(){
var firebaseRef = firebase.database().ref().child();//here in .child() i need to pass the areaCode as an argument. It works when i hardcode a value like .child('1234'). But i need that to be what ever the value I am getting from the textbox of areaCode. I have tried //var x = areaCode.value; // var y = x.toString and var y = String(x); , trying to pass y as .child(y)
// also var y = areaCode.toString(); does not work.
var toTimeText = toTime.value;
firebaseRef.child("toTime").set(toTimeText);
window.confirm("Data has been successfully sent.");
}
Any help highly appreciated.

try this
function sumbitClick(){
var toTimeText = document.getElementById("toTime").value;
var ref = firebase.database().ref(path_to_your_child);
ref.once("value")
.then(function(snapshot) {
ref.set(toTimeText);
});
}

Related

TypeError: Cannot read property "values" from undefined. onEdit # ClosedTSR.gs:3

// When Form Gets edited
function onEdit(e) {
var Timestamp = e.values[2];
var email_sender = e.values[3];
var email_address = "";
var TSRNumber = e.values[19];
var IssueType = e.values[4];
var Region = e.values[0];
var Customer = e.values[5];
var ShortName = e.values[6];
var City = e.values[9];
var State = e.values[10];
var Contact = e.values[11];
var Product = e.values[12];
var TankCode = e.values[13];
var ProbDesc = e.values[14];
var Terminal = e.values[1];
var Priority = e.values[18];
var TSRStatus = e.values[22];
// Check if TSR is Closed
if(TSRStatus == "Closed"){
switch (Region) {
case "East":
email_address = xxxxxxxx
Basically when a user changes a cell value to "Closed" I am trying to send an email containing information from that specific row.
There is no values array in an onEdit trigger event object. There is for an onFormSubmit trigger.
event objects
I recommend reading before coding or at least during coding

JS Function URL Parameters - Null Value [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 2 years ago.
I am trying to pull URL parameters to pass to a prebuilt HTML form.
The example URL is:
?state=CA&**period**=PERIOD_FIXED_30YEARS&**loan**=200000&ltv=80&**transaction**=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005
The text in bold is what I would need to pull.
I was trying to do this through:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Gathering the URL Parameters
var period = getParameterByName('period');
var loan = getParameterByName('loan');
var ltv = getParameterByName('ltv');
var transaction = getParameterByName('transaction');
var property_type = getParameterByName('property_type');
var fico = getParameterByName('fico');
var occupancy = getParameterByName('occupancy');
var cashout = getParameterByName('cashout');
var rate = getParameterByName('rate');
var fees = getParameterByName('fees');
var points = getParameterByName('points');
var trackingID = getParameterByName('trackingID');
And then assign to the form using
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
I keep getting a null error and I think it is the regex.
Any help would be appreciated. I searched similar questions on the site and none of the answers worked in my situation.
UPDATING WITH WORKING CODE:
<script type="text/javascript">
//Used to pull URL
const link = window.location.href;
const url = new URL(link);
//Function to get the URL parameters
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters and writing to console to check if parse is accurate -- Note the console.log can be removed if needed
var period = getParameterByName('period');
console.log(period);
var loan = getParameterByName('loan');
console.log(loan);
var ltv = getParameterByName('ltv');
console.log(ltv);
var transaction = getParameterByName('transaction');
console.log(transaction);
var property_type = getParameterByName('property_type');
console.log(property_type);
var fico = getParameterByName('fico');
console.log(fico);
var occupancy = getParameterByName('occupancy');
console.log(occupancy);
var cashout = getParameterByName('cashout');
console.log(cashout);
var rate = getParameterByName('rate');
console.log(rate);
var fees = getParameterByName('fees');
console.log(fees);
var points = getParameterByName('points');
console.log(points);
var trackingID = getParameterByName('trackingID');
console.log(trackingID);
//On load function to prefill the forms hidden fields
$( document ).ready(function() {
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
document.getElementById("tfa_51").value = ltv;
document.getElementById("tfa_53").value = transaction;
document.getElementById("tfa_54").value = property_type;
document.getElementById("tfa_55").value = fico;
document.getElementById("tfa_56").value = occupancy;
document.getElementById("tfa_57").value = cashout;
document.getElementById("tfa_58").value = rate;
document.getElementById("tfa_59").value = fees;
document.getElementById("tfa_60").value = points;
document.getElementById("tfa_61").value = trackingID;
});
</script>
You can create an URL object and then use the url.searchParams.get(param) method to get the desired parameter. More information.
Working example:
const link = "http://www.example.com/test.html?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000&ltv=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005";
const url = new URL(link);
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters
var period = getParameterByName('period');
console.log(period);
The hardcoded url is just for the example with your URL, in your code use:
const url = window.location.href

Automatically push / nest object within a parent object in JavaScript

I am currently working on an excel like behaviour for some task.
All "tr"s are bound to the variable $test
var $test = $('tbody tr');
Now I use the jQuery .each function to run throuhg all trs and collect its relevant contents like this :
$test.each(function(index, element){
var $row_id = $(this).data("rowid");
var status = $(this).find('#status option:selected').val();
var ma_name = $(this).find('#ma-name').val();
var datum = $(this).find('#datum').val();
var firmenname1 = $(this).find('#firmenname1').val();
var firmenname2 = $(this).find('#firmenname2').val();
var limit = $(this).find('#limit').val();
var gruppe_kredit = $(this).find('#gruppe_kredit').val();
var omv_kdnr = $(this).find('#omv_kdnr').val();
var sap_kdnr = $(this).find('#sap_kdnr').val();
var fos = $(this).find('#fos').val();
var hga_kdnr = $(this).find('#fos').val();
var pushObj = {row_id: $row_id,....};
});
Since the pushObject gets greated and stuffed with content each time the each function gets executed I need a way to "push" this object into a parent object which I can later use with AJAX.
The behaviour I'd wish for would be like this:
var parentObj = {};
$.each(function(){
// in each run the newly created object gets nested into the parentObject which results in the parent object having X-childObjects
});
So after, lets say 5 each runs, the parentObj should contain : [0],[1],...,[4] objects.
Could anyone guide me throuhg that process ?
Try this solution by storing pushObj inside array variable like so :
// add here
var parentObj = [];
$test.each(function (index, element) {
var $row_id = $(this).data("rowid");
var status = $(this).find('#status option:selected').val();
var ma_name = $(this).find('#ma-name').val();
var datum = $(this).find('#datum').val();
var firmenname1 = $(this).find('#firmenname1').val();
var firmenname2 = $(this).find('#firmenname2').val();
var limit = $(this).find('#limit').val();
var gruppe_kredit = $(this).find('#gruppe_kredit').val();
var omv_kdnr = $(this).find('#omv_kdnr').val();
var sap_kdnr = $(this).find('#sap_kdnr').val();
var fos = $(this).find('#fos').val();
var hga_kdnr = $(this).find('#fos').val();
var pushObj = {
row_id: $row_id,
....
};
// add here
parentObj.push(pushObj);
});
// later on here access parentObj variable

How to make this jQuery code more simple and effective

I have follow two functions here which working great!
As you can see these two functions are almost the same, except the code which comes below the last comment in each function.
How do I make that more simple. Could I make a code-"holder" - Where I only include a part of a code from another file? So I don't have too have the "same" code in each functions?
Should I use some kind of classes or? - I have never worked with classes.
/// Function (add_new_field)
$(document).on("click", '.add_new_field', function(e) {
e.preventDefault();
var flex0 = $(this);
var flex1 = $(this).parent().closest('div');
var flex2 = $(flex1).parent().closest('div');
var flex3 = $(flex2).parent().closest('div');
var flex4 = $(flex3).parent().closest('div');
var flex5 = $(flex4).parent().closest('div');
var flex6 = $(flex5).parent().closest('div');
/*console.log(
' -> WrapID:'+flex6.attr('id')+
' -> accordionContentID:'+flex5.attr('id')+
' -> acContentBoxID:'+flex4.attr('id')+
' -> acChildBoxID:'+flex3.attr('id')+
' -> acBabyBoxID:'+flex2.attr('id')+
' -> SecondID:'+flex1.attr('id')+
' -> FirstID:'+flex0.attr('id')
);*/
var wrapID = flex6.attr('id'); // wrapID
var accordionContentID = flex5.attr('id');
var acContentBoxID = flex4.attr('id'); // sharedID
var acChildBoxID = flex3.attr('id'); // langID
var acBabyBoxID = flex2.attr('id'); // langID
var SecondID = flex1.attr('id'); // OLD : AddLangBoxID
var FirstID = flex0.attr('id'); // OLD : add_new_fieldID
// there is a lot more code here...
)};
/// Function (del_field)
$(document).on("click", '.del_field', function(e) {
e.preventDefault();
var flex0 = $(this);
var flex1 = $(this).parent().closest('div');
var flex2 = $(flex1).parent().closest('div');
var flex3 = $(flex2).parent().closest('div');
var flex4 = $(flex3).parent().closest('div');
var flex5 = $(flex4).parent().closest('div');
var flex6 = $(flex5).parent().closest('div');
var wrapID = flex6.attr('id'); // wrapID
var accordionContentID = flex5.attr('id');
var acContentBoxID = flex4.attr('id'); // sharedID
var acChildBoxID = flex3.attr('id'); // langID
var acBabyBoxID = flex2.attr('id'); // langID
var SecondID = flex1.attr('id'); // OLD : AddLangBoxID
var FirstID = flex0.attr('id'); // OLD : add_new_fieldID
// there is a lot more code her
)};
How could I make something like this.
/// I want to include this code into the functions. So I don't have to write it twice.
var flex0 = $(this);
var flex1 = $(this).parent().closest('div');
var flex2 = $(flex1).parent().closest('div');
var flex3 = $(flex2).parent().closest('div');
var flex4 = $(flex3).parent().closest('div');
var flex5 = $(flex4).parent().closest('div');
var flex6 = $(flex5).parent().closest('div');
var wrapID = flex6.attr('id'); // wrapID
var accordionContentID = flex5.attr('id');
var acContentBoxID = flex4.attr('id'); // sharedID
var acChildBoxID = flex3.attr('id'); // langID
var acBabyBoxID = flex2.attr('id'); // langID
var SecondID = flex1.attr('id'); // OLD : AddLangBoxID
var FirstID = flex0.attr('id'); // OLD : add_new_fieldID
Thank you.
If you want to do something like classes in javascript, your best bet is to use prototyping, since javascript doesn't have classes.
In this case, I'm not sure what your code does, so prototyping might not be the best answer. Your other option is to encapsulate all of your variable definitions in another function, and call that function in both of your click functions. It would return an object with attributes, rather that a bunch of vars.
var myVarFn = function(){
var returnObject = {};
returnObject.flex0 = $(this);
returnObject.flex1 = $(this).parent().closest('div');
...
returnObject.wrapID = flex6.attr('id'); // wrapID
...
return returnOBject
}
And to use it in click, with this defined the same as in the scope of the click function:
$(document).on("click", '.del_field', function(e) {
var dataObject = myVarFn.call(this);
//Other code
};
And in the other click event,
$(document).on("click", '.add_new_field', function(e) {
var dataObject = myVarFn.call(this);
//Other code
};
You will have to modify your other code to use dataObject.flex0 instead of flex0 and so on.
Create a global object
var allId = new Object();
And a function that can be called whenever needed..
function getAllIDs(el) {
var id = ["wrapID", "accordionContentID", "acContentBoxID", "acChildBoxID", "acBabyBoxID", "SecondID", "FirstID"];
var flex;
for (var i = 0; i <= 6; i++) {
if (i == 0) {
flex = $(el).parent().closest('div');
} else {
flex = $(flex).parent().closest('div');
}
allId.id[i] = $(flex).parent().closest('div').attr('id');
}
}
Further, on jQuery event you can call getAllIDs function
$(document).on("click", '.add_new_field', function (e) {
e.preventDefault();
getAllIDs(this);
});
More details about javascript objects

How to get the parameter value from URL in Jquery?

Hi all i have an url where i need to get an parameter from the url
var URL="http://localhost:17775/Students/199/Kishore"
//here from the url i need to get the value 199
this is what i had been trying but the value is null here
function getURLParameter(name) {
return parent.decodeURI((parent.RegExp(name + /([^\/]+)(?=\.\w+$)/).exec(parent.location.href) || [, null])[1]);
};
$(document).ready(function() {
getURLParameter("Students");
//i need to get the value 199 from the url
});
jQuery is not needed for this, though it could be used. There are lots of ways to skin this cat. Something like this should get you started in the right direction:
var URL="http://localhost:17775/Students/199/Kishore";
var splitURL = URL.split("/");
var studentValue = "";
for(var i = 0; i < splitURL.length; i++) {
if(splitURL[i] == "Students") {
studentValue = splitURL[i + 1];
break;
}
}
Here's a working fiddle.
Edit
Based on the comments, indicating that the position will always be the same, the extraction is as simple as:
var url = "http://localhost:17775/Students/199/Kishore";
var studentValue = url.split("/")[4];
This is what you're looking for since the URL parameter will keep changing:
http://jsbin.com/iliyut/2/
var URL="http://localhost:17775/Students/199/Kishore"
var number = getNumber('Students'); //199
var URL="http://localhost:17775/Teachers/234/Kumar"
var number = getNumber('Teachers'); //234
function getNumber(section) {
var re = new RegExp(section + "\/(.*)\/","gi");
var match = re.exec(URL);
return match[1];
}
I would do the following:
var url = "http://localhost:17775/Students/199/Kishore";
var studentValue = url.match('/Students/(\\d+)/')[1]; //199

Categories

Resources