1

i want to use the value of a php variable within a jquery ajax function. i however first need to test if the variable has value.

i know that i can test the length of a object like this:

if ($(selector).length)

i am not sure how i can test if a jquery variable has value. below is my test;

 $idAjax = 15;

<script>
jQuery(document).ready(function(){

 function ajaxMessagesCount(){   

      var  theData =  <?php echo $idAjax ?>;

  if (theData.length){
//do somting
}

})
</script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Paul Kendal
  • 559
  • 9
  • 24

1 Answers1

0

If you want to check if variable exists, and it's not empty, try this:

if (typeof variable !== 'undefined' && variable)
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
Daimos
  • 1,473
  • 10
  • 28