Get a form input field value with javascript

You can use document.getElementById("input_field_id"); or you can use:

var form=document.forms["form_name"];
var input_value= form["input_field_id"].value;

Note that the typeof(input_value) will be string even if the type of the input field is “number”.

So even if you enter an integer in the field Number.isInteger(input_value) will return false and typeof(input_value) will return string.

Check if a function is declared before using it in js

Issue: it may occurs that you have to call a function that is conditionally set. A frequent use case would be GDPR where you set your tracking script conditionally. You micht check the cookie for each event you track, but I think a better solution would be to track if the function is declared.


There are two different options:

Option 1:

This option will still return an error in case the function to evaluate is not defined.

function isFunction(TrackingScript) {
   TrackingScript ('button-click','cart',10);

Option 2:

if (typeof( TrackingScript ) === typeof(Function)){
       TrackingScript ('button-click','cart',10);