I'm attempting to check the values entered by a user into an input text field.  Once they enter the value and click submit, if the value they enter matches a value in an array, I'm attempting to produce one result, if not in the array, another result.  Basically have a simple form like so:  &lt;form id="checkinput" action="#"&gt; &lt;input type="text" value="" id="couponcode" name="coupon code name"/&gt; &lt;input type="button" id="submit" value="Submit"/&gt; &lt;/form&gt;   When click "#submit", if value in the "#couponcode" input field equals the value in an array, add a class of "success" to a div with an id of "success_show" otherwise a simple alert like "your coupon code is wrong" would be displayed.  I'll have a hidden div and upon success, add a class of "success" to the div.  The array will change periodically and will have different quantities at different times.  Something simple like so:  var arr = [ "CA238", "Pete", 8, "John" ];
717
What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) {     for (var i = 0; i &lt; a.length; i++) {         if (a[i] === obj) {             return true;         }     }     return false; }  Is there a better and more concise way to accomplish this?
81188
Just wondering if there is anything built-in to Javascript that can take a Form and return the query parameters, eg: "var1=value&amp;var2=value2&amp;arr[]=foo&amp;arr[]=bar..."  I've been wondering this for years.
38310
Given the following classes and controller action method:  public School {   public Int32 ID { get; set; }   publig String Name { get; set; }   public Address Address { get; set; } }  public class Address {   public string Street1 { get; set; }   public string City { get; set; }   public String ZipCode { get; set; }   public String State { get; set; }   public String Country { get; set; } }  [Authorize(Roles = "SchoolEditor")] [AcceptVerbs(HttpVerbs.Post)] public SchoolResponse Edit(Int32 id, FormCollection form) {   School school = GetSchoolFromRepository(id);    UpdateModel(school, form);    return new SchoolResponse() { School = school }; }   And the following form:  &lt;form method="post"&gt;   School: &lt;%= Html.TextBox("Name") %&gt;&lt;br /&gt;   Street: &lt;%= Html.TextBox("Address.Street") %&gt;&lt;br /&gt;   City:  &lt;%= Html.TextBox("Address.City") %&gt;&lt;br /&gt;   Zip Code: &lt;%= Html.TextBox("Address.ZipCode") %&gt;&lt;br /&gt;   Sate: &lt;select id="Address.State"&gt;&lt;/select&gt;&lt;br /&gt;   Country: &lt;select id="Address.Country"&gt;&lt;/select&gt;&lt;br /&gt; &lt;/form&gt;   I am able to update both the School instance and the Address member of the school.  This is quite nice!  Thank you ASP.NET MVC team!  However, how do I use jQuery to select the drop down list so that I can pre-fill it?  I realize that I could do this server side but there will be other dynamic elements on the page that affect the list.  The following is what I have so far, and it does not work as the selectors don't seem to match the IDs:  $(function() {   $.getJSON("/Location/GetCountryList", null, function(data) {     $("#Address.Country").fillSelect(data);   });   $("#Address.Country").change(function() {     $.getJSON("/Location/GetRegionsForCountry", { country: $(this).val() }, function(data) {       $("#Address.State").fillSelect(data);     });   }); });
88238
I have problem in some JavaScript that I am writing where the Switch statement does not seem to be working as expected.  switch (msg.ResultType) {   case 0:     $('#txtConsole').val("Some Val 0");     break;   case 1:     $('#txtConsole').val("Some Val 1");     break;   case 2:     $('#txtConsole').text("Some Val 2");     break; }   The ResultType is an integer value 0-2 and I can see that in FireBug.  In all cases, the switch transfers control to the final break statement which means all the logic is completely skipped.  What am I missing?
102259
I need to search a string in the string array. I dont want to use any for looping in it  string [] arr = {"One","Two","Three"};  string theString = "One"   I need to check whether theString variable is present in arr.
96449
I am trying to create a formula that will put a 1 in a hidden field if the value of a picklist is equal to a specific value.  I keep getting stuck here and I can't figure out what I am missing?  IF(ISPICKVAL( Examiner_Type__c ="Pending Examiner Status","1","",))
32885
I have some html like this:  &lt;div id="1"&gt;     &lt;p&gt;         Volume = &lt;input type="text" /&gt;         &lt;button rel="3.93e-6" class="1" type="button"&gt;Check answer&lt;/button&gt;     &lt;/p&gt;     &lt;div&gt;&lt;/div&gt; &lt;/div&gt;   and some JS like this:  $("button").click(function () {     var buttonNo = $(this).attr('class');     var correct = Number($(this).attr('rel'));      validate (Number($("#"+buttonNo+" input").val()),correct);     $("#"+buttonNo+" div").html(feedback); });   What I'd really like is if I didn't have to have the class="1" on the button (I know numeric classes aren't valid, but this is a WIP!), so I could determine buttonNo based on the id of the parent div. In real life there are multiple sections looking like this.   How do I find the id of the div parenting the button. What would be a more semantic way to store the answer in the button code. I want to make this as foolproof as possible for a non programmer to copy and paste without breaking things!
8481
What is the easiest way to encode a PHP string for output to a JavaScript variable?  I have a PHP string which includes quotes and newlines.  I need the contents of this string to be put into a JavaScript variable.  Normally, I would just construct my JavaScript in a PHP file, à la:  &lt;script&gt;   var myvar = "&lt;?php echo $myVarValue;?&gt;"; &lt;/script&gt;   However, this doesn't work when $myVarValue contains quotes or newlines.
46286
On a web form, what should happen when the user presses the ENTER key?  I'm writing a sign-up form - name, email, password, sign-up button: the usual sort of thing. Since I'm using JavaScript, and the page does not use the &lt;form...&gt; tag, I have complete control over what happens with each key press. I can make it change the focus, submit the data to the server, validate the field contents, whatever. But what should it do?  Things I'm wondering: should ENTER take the user to the next field (as per TAB key)? Should pressing ENTER while focus is on the final field submit the form?
54866
I want to grab the value of a hidden input field in HTML.  &lt;input type="hidden" name="fooId" value="12-3456789-1111111111" /&gt;   I want to write a regular expression in Python that will return the value of fooId, given that I know the line in the HTML follows the format  &lt;input type="hidden" name="fooId" value="**[id is here]**" /&gt;   Can someone provide an example in Python to parse the HTML for the value?
40506
I am writing a function that adds an ID number to an array and puts the array in the usermeta. $_GET['auction'] is the post_id.  Below is the function:  $reminders = get_user_meta( $current_user-&gt;ID, "reminders" ); print_r( $reminders ); if( in_array( $_GET['auction'], $reminders ) ) {     echo "Failed: Auction already in list"; } else {     array_push( $reminders, intval( $_GET['auction'] ) );     if ( update_user_meta( $current_user-&gt;ID, "reminders", $reminders ) ) {         echo "Success";     } else {         echo "Failed: Could not update user meta";     } } print_r( $reminders );   Here is the output after adding one auction:  Array ( )  Success Array ( [0] =&gt; 7 )    Here is the output after adding two auctions:  Array ( [0] =&gt; Array ( [0] =&gt; 7 ) )  Success Array ( [0] =&gt; Array ( [0] =&gt; 7 ) [1] =&gt; 73 )   And here is the output after adding three auctions:  Array ( [0] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; 7 ) [1] =&gt; 73 ) )  Success Array ( [0] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; 7 ) [1] =&gt; 73 ) [1] =&gt; 0 )   Notice that adding a new element to the array using array_push works fine. But when the array is stored in the usermeta then retrieved again, the last element in the array has been put into an array of its own, creating an infinitely dimensional array. I'd prefer to keep this array one dimensional.  Is there a way I can run update_user_meta and get_user_meta without it changing the structure of my array?
70640
I have two pages of jQuery, Page1 and Page2, and I'm able to get input in Page1.  The somval=1000$.  The page 1 user enters the somevalue. I have stored the value:  var val = somval;   Now in the second page, I need to get the result of somvalue in page 1. Of course two pages using My1.js My2.js respectively.  How do I share the values from one jQuery file to other JavaScript or how do I get the value from page1 value, to page2?  How do I tackle this?
75034
I have an input form that lets me select from multiple options, and do something when the user changes the selection. Eg,  &lt;select onChange="javascript:doSomething();"&gt;   &lt;option&gt;A&lt;/option&gt;   &lt;option&gt;B&lt;/option&gt;   &lt;option&gt;C&lt;/option&gt; &lt;/select&gt;   Now, doSomething() only gets triggered when the selection changes.  I want to trigger doSomething() when the user selects any option, possibly the same one again.  I have tried using an "onClick" handler, but that gets triggered before the user starts the selection process.  So, is there a way to trigger a function on every select by the user?  Update:  The answer suggested by Darryl seemed to work, but it doesn't work consistently. Sometimes the event gets triggered as soon as user clicks the drop-down menu, even before the user has finished the selection process!
2878
I want to use a PHP variable in JavaScript. How is it possible?
15800
Well I am trying to submit a form by pressing enter but not displaying a submit button. I don't want to get into JavaScript if possible since I want everything to work on all browsers (the only JS way I know is with events).  Right now the form looks like this:  &lt;form name="loginBox" target="#here" method="post"&gt;     &lt;input name="username" type="text" /&gt;&lt;br /&gt;     &lt;input name="password" type="password" /&gt;     &lt;input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" hidefocus="true" /&gt; &lt;/form&gt;   Which works pretty well. The submit button works when the user presses enter, and the button doesn't show in Firefox, IE, Safari, Opera and Chrome. However, I still don't like the solution since it is hard to know whether it will work on all platforms with all browsers.  Can anyone suggest a better method? Or is this about as good as it gets?
83806
currently, I'm passing in a options array to the options attribute of a lightning-checkbox-group. I need to mimic the behavior of that tag, but I need to do it in html because I don't think I can get the checkboxes to display horizontally. I've tried &lt;template for:each and have not had any luck. My goal would be to pass in the same name/value pair array, loop through it and call the same change handler that is called when using lightning-checkbox-group. A simple code snippet would be awesome. This is what I've tried so far: {lineItemObjectInfo.data.fields.Reason__c.label}  &lt;template for:each={reasonOptions} for:item=&quot;option&quot; for:index=&quot;index&quot;&gt;      &lt;div key={option}&gt;          &lt;lightning-input onchange={handleChange}                            name={lineItemObjectInfo.data.fields.Reason__c.apiName}                            type=&quot;checkbox&quot; label={option.label}                            value={recordInput.fields.Reason__c}&gt;&lt;/lightning-input&gt;      &lt;/div&gt;  &lt;/template&gt;  That actually gets me pretty close. When changed it fires the change handler and fails when I'm trying to do a .join on the recordInput for the field: handleChange(event) {     const {target: {name, value}} = event;        switch (name ) {         case &quot;Reason__c&quot;:             this.recordInput.fields[name] = value.join(&quot;;&quot;);             break;         default:             this.recordInput.fields[name] = value;             break;     } }  I've also tried using a form tag with input and label tags but that didn't get me much further. The switch has been reduced for purpose of making it more clear. This is my original use of lightning-checkbox-group which functionally worked but styling is inflexible: &lt;div class=&quot;slds-col slds-size_3-of-12&quot;&gt;     &lt;lightning-checkbox-group     onchange={handleChange}     name={lineItemObjectInfo.data.fields.Reason__c.apiName}     label={lineItemObjectInfo.data.fields.Reason__c.label}     options={reasonOptions}     value={recordInput.fields.Reason__c}&gt;&lt;/lightning-checkbox-group&gt; &lt;/div&gt;
55833
I have some client-side validation against a text box, which only allows numbers up to two decimal places with no other input.  This script was a basis for entering numeric values only, however it needs to be adapted so it can take a decimal point followed by only up to two decimal places.  I've tried things such as /[^\d].\d{0,2}, but then the replacement call wouldn't work, and I've got no idea how to do it.  Code  &lt;script type="text/JavaScript"&gt;   function valid(f) {     if (!/^\d*$/.test(f.value)) {       f.value = f.value.replace(/[^\d]/g,"");       alert("Invalid number");     }   } &lt;/script&gt;     Note  I need to match an empty string. If an empty string is provided and the form is submitted, the value defaults back to zero.