title: pass variable value of the clicked button (not button in a form) to ajax url?
body: <p>In idx.php head tag, I have a script like this :<br></p>
  <pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
  $(document).ready(function(){
  var alamat=&quot;1.php&quot;;
  //$('button[name=&quot;test&quot;]').click(function(){
  //var alamat= $(this).val();
  //alert(alamat);
  //});
      
      $('.search-box input[type=&quot;text&quot;]').on(&quot;keyup input&quot;, function(){
          /* Get input value on change */
          var inputVal = $(this).val();
          var resultDropdown = $(this).siblings(&quot;.result&quot;);
          if(inputVal.length){
              $.get(&quot;backend-search.php&quot;, {term: inputVal}).done(function(data){
                  // Display the returned data in browser
                  resultDropdown.html(data);
              });
          } else{
              resultDropdown.empty();
          }
      });
      
      // Set search input value on click of result item
      $(document).on(&quot;click&quot;, &quot;.result p&quot;, function(){
          $(this).parents(&quot;.search-box&quot;).find('input[type=&quot;text&quot;]').val('');
                  $.ajax({
                  type: &quot;POST&quot;,
                  //url: &quot;1.php&quot;,
                  url: alamat,
                  data: {name: $(this).text()},
                  success: function(msg){
                      $(&quot;#hasil&quot;).html(msg)
                  },
                  error: function(){
                      alert(&quot;failure&quot;);
                  }
              });
          $(this).parent(&quot;.result&quot;).empty();
      });
  });
  &lt;/script&gt;
  </code></pre>
  <p>And in the same idx.php, in the body tag, I have this :<br></p>
  <pre><code>  &lt;button name=&quot;test&quot; class=&quot;btn&quot; value=&quot;1.php&quot;&gt;&lt;a href=&quot;idx.php&quot;&gt;THIS&lt;/a&gt;&lt;/button&gt;
    &lt;button name=&quot;test&quot; class=&quot;btn&quot; value=&quot;2.php&quot;&gt;THAT&lt;/button&gt;
  </code></pre>
  <p>In the 2nd line of the script, I hardcoded the variable alamat value as &quot;1.php&quot;<br>
  then in $ajax for the url I use that alamat variable value.<br>
  The code runs as expected.</p>
  <p>Now I'm trying to have the alamat variable is dynamic based on which button is clicked.<br>
  So I commented (//) the 2nd line of the script, and uncommented the 3rd to the sixth line.</p>
  <p>When I click the first button, I see the alert window show &quot;1.php&quot;<br>
  and when I click the second button, I see the alert window show &quot;2.php&quot;.<br>
  So I thought the alamat variable has a value, just like as if I hardcoded it.</p>
  <p>But it seems the $ajax url has no value as it's just stand still after I click one of the &quot;.result p&quot; list. So it seems the alamat variable value from the clicked button is not passing to $ajax url call.</p>
  <p>So my question is :<br>
  How do I code it in order to have the $ajax url value can be either &quot;1.php&quot; or &quot;2.php&quot; based on the clicked button ?</p>
  <p>or<br>
  how to have the alamat variable value (resulted from the 3rd to the sixth line) go on to $ajax <code>url:alamat</code> ?</p>