Value does not change in javascript variable in onClick event

I am trying to implement jquery and php dynamic search, but I cannot change the value of the javascript variable in my onclick event.

<script type="text/javascript">
  var main_category = '<?php echo $main_category?>';
  var main_url = '<?php echo $url?>get/' + encodeURIComponent(main_category) + '/';
  var container_id = '<?php echo $unique_id?>content_area';

    //name
    $('#<?php echo $unique_id?>search_by_name').autocomplete('<?php echo $url?>get_autocomplete_data/' + encodeURIComponent(main_category) + '/names/', {
      matchContains: "word",
      autoFill: true,
      width: 310
    });$('#<?php echo $unique_id?>search_by_name').result(function (event, data){
      var url = main_url + 'search/title/' + encodeURIComponent(data);
      load_div(container_id, url);
    });


    //on click of displayed categories
    $('[rel="<?php echo $unique_id?>sub_category"]').click(function (){
      window['main_category'] = this.title;
      $('#<?php echo $unique_id?>category').html('' +main_category+ '');
      return false;
    });
  });
</script>

      

Changes the value on click on click

//on click of displayed categories
        $('[rel="<?php echo $unique_id?>sub_category"]').click(function (){
          window['main_category'] = this.title;
          $('#<?php echo $unique_id?>category').html('' +main_category+ '');
          return false;
        });

      

but when after that I add data to search, it still looks for the old category

$('#<?php echo $unique_id?>search_by_name').result(function (event, data){
          var url = main_url + 'search/title/' + encodeURIComponent(data);
          load_div(container_id, url);
        });

      

The "main_category" value does not change in "main_url"

+2


a source to share


1 answer


main_url is a variable and it is set only once. Changing the value of another variable used when setting it does not change its value. You have to reset main_url. Or better yet, make main_url a function that returns a given value.



0


a source







All Articles