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
  • 155,085
  • 27
  • 166
  • 222
Paul Kendal
  • 547
  • 7
  • 20

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
  • 23,635
  • 4
  • 32
  • 50
Daimos
  • 1,473
  • 10
  • 27