0

I have the following html, which is part of a webform:

<input type="hidden" name="userID" id="control_COLUMN43" value="%%userID%%">

The value of this field is dynamically generated by a database. It's possible that the value of this field is empty.

Next to this, I created a function which sends the value of this field (via Ajax) to another database, upon a submit of the webform.

What I want to do now is: only execute this function if the value of the field "userID" is NOT empty. If it's empty, I don't want this function to be executed.

So I assume it will be something like this, but I'm struggling to find the correct way to do this:

if (#control_COLUMN43 !=== "") //this is the id of the field
{
function SendAjax()
   {
      if
      {
        //code
      }
      else
      {
        //code
      }
    }
}
else
{
//don't execute function
}

Or the other way around I guess?

Can anybody help me with this?

Thanks in advance!

Loren
  • 37
  • 2
  • 9

11 Answers11

1

Use like this

    // Also u can add null check
        if(data !== '') {
   // do something
}

If, however you just want to make sure, that a code will run only for "reasonable" values, then you can, as others have stated already, write:

if (data) {
  // do something
}

Since, in javascript, both null values, and empty strings, equals to false (i.e. null == false).

The difference between those 2 parts of code is that, for the first one, every value that is not specifically an empty string, will enter the if. But, on the second one, every true-ish value will enter the if: false, 0, null, undefined and empty strings, would not.

0
You can check empty value like this





    function isEmpty(str) {
         return (!str || 0 === str.length);
    }

    var val = document.getElementById('control_COLUMN43').value;
    var col = isEmpty(val);
    if (col) {
     function SendAjax(){
      if
      {
        //code
      }
      else
      {
        //code
      }
    }
}
else
{
//don't execute function
}
Sanjay Kumar
  • 424
  • 5
  • 15
0
// get the contents of the form element
var control_COLUMN43 = document.getElementById("control_COLUMN43").value;

// process the data
if( control_COLUMN43 != "" ) {......
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • @Luca I would've thought that the comments within the provided code would be sufficient to let the OP "know" what I was suggesting. –  Jun 02 '18 at 13:01
0

There are several issues at once:

  • You are mixing up declaring a function with calling it.
  • To get the value from a control, use document.getElementById("...").value
  • The proper notation for not === is !==.

This is how it goes:

// Declare the function
function SendAjax()
{
    if
    {
        //code
    }
    else
    {
        //code
    }
}

// Get value
var value = document.getElementById("control_COLUMN43").value;

// Call the function conditionally
if (value !== "")
{
    SendAjax();
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
0

You should not declare functions inside the conditions. If you do then at the time of execution if the condition is not met, function will not be available which may lead to errors. Hence, you should place the condition inside the function.

You can modify your condition to following

function SendAjax() {
    if (document.getEelementById("control_COLUMN43").value) {
        //code
    }
}
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
0

You can access the value of the input by using getElementById(id).value and declare your function outside the if block like:

function SendAjax()
   {
      if
      {
        //code
      }
      else
      {
        //code
      }
    }

if (document.getElementById('txt_name').value) //this is the id of the field
{
  SendAjax()
}
else
{
//don't execute function
}
supra28
  • 1,646
  • 10
  • 17
0

The if statement you need, without JQuery, should be like this:

if (document.getElementById("control_COLUMN43").value !=== "") {
   // .. commands
}
Guilherme Mussi
  • 956
  • 7
  • 14
0

Basically you have to check value property of the input element. Using dom selectors first you can select a element using id then you can access value attribute for element. If it's not null or undefined or just empty spaces then you can call your function

function handleClick(){
  // Extract value from input element
  let val = document.getElementById('input').value;
  // Check value is empty or not
  if(val){
    // If not empty
    console.log('Value is : ' + val);
  }
  else{
    // If empty
    console.log('Empty input');
  }
}
<input id="input" type="text">
<button onclick="handleClick()">SUBMIT</button>
nerding_it
  • 704
  • 7
  • 17
0

First get your hidden input value using document.getElementById() and then check if it is null like following:

var userId = document.getElementById("control_COLUMN43");
if (userId) //this is the id of the field
{
   SendAjax()
}
else
{
  alert("UserId is null);
}

function SendAjax()
   {
      if
      {
        //code
      }
      else
      {
        //code
      }
    }
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
0

The code you write in if condition is not correct ID value must be get like:

if(document.getElementById("control_COLUMN43").value != ''){
    //Your code here
}
0

In if condition check $("#control_COLUMN43").val() It can be null or '' so you can apply condition accordingly.