Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing jquery ui autocomplete selection into variable
    text
    copied!<p>I have a JQuery function to change the color of a button while adding or removing a <code>href</code> attribute to it. It works perfectly if I initiate it with blur like</p> <pre><code>$("#name").blur(function() { var standards_name = $("#name").val(); var standards_link = $("#standardslink"); var update_log = $("#update_log"); if(jQuery.inArray(standards_name, standards) != -1) { if(companyid !== null) { standards_link.attr("href", basic_href + "?standards_name=" + standards_name + "&amp;company_id=" + companyid); } else { standards_link.attr("href", basic_href + "?standards_name=" + standards_name); } standards_link.css("color","#2EE52B"); } else if(standards_name == "") { standards_link.removeAttr("href"); standards_link.css("color","black"); update_log.hide(); } else { standards_link.removeAttr("href"); standards_link.css("color","#FF0011"); update_log.hide(); } }); </code></pre> <p>but would like it to be initiated on the very selection from the autocomplete drop-down list. This is where I can't get any further. The pasted code works if I manually set the variable "standards_name", e.g. <code>var standards_name = "xxxx";</code> but I want the <code>ui.item.ticker_name</code> value to be passed to the variable. (The "standards" array and "companyid" are set with <code>PHP</code> <code>json_encode</code> and passed earlier in the script and works just fine)</p> <p>Here's the code:</p> <pre><code>$(function() { $("#name").autocomplete({ source: "../autocomplete.php", minLength: 2, select: function(event, ui) { $('#name').val(ui.item.ticker_name); $('#mktcap').val(ui.item.mkt_cap); var standards_name = ui.item.ticker_name; var standards_link = $("#standardslink"); var update_log = $("#update_log"); if(jQuery.inArray(standards_name, standards) != -1) { if(companyid !== null) { standards_link.attr("href", basic_href + "?standards_name=" + standards_name + "&amp;company_id=" + companyid); } else { standards_link.attr("href", basic_href + "?standards_name=" + standards_name); } standards_link.css("color","#2EE52B"); } else if(standards_name == "") { standards_link.removeAttr("href"); standards_link.css("color","black"); update_log.hide(); } else { standards_link.removeAttr("href"); standards_link.css("color","#FF0011"); update_log.hide(); } } }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload