logo
down
shadow

JQuery on change fired only after second change of select box


JQuery on change fired only after second change of select box

Content Index :

JQuery on change fired only after second change of select box
Tag : javascript , By : Pip
Date : November 28 2020, 04:01 AM

To fix this issue You are adding the logic using on change, so the first time that will not yet be present.
You might just use the loop instead because that is already using the id's of the selects:
$(document).ready(function() {
  var slots = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
  $.each(slots, function(index, value) {
    $('#slot' + value).change(function() {
      if ($('#slot' + value).val() === 'NULL') {
        switch (value) {
          case '1':
            var v = 'Slot\n1';
            break;
          case '2':
            var v = 'Slot\n2';
            break;
            // ...
        }
        $('td.slot' + value).text(v);
      } else {
        $('td.slot' + value).text('');
        $('td.slot' + value).prepend('<img src="https://img.icons8.com/material/24/000000/console.png" style="width: 100%; height: 100%; object-fit: contain"\>');
      }
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td class="slot1">Slot<br>1</td>
      <td class="slot2">Slot<br>2</td>
    </tr>
  </tbody>
  <div class="selects">
    <label>Slot 1</label>
    <select id="slot1" name="slot1">
      <option value="NULL">---Select---</option>
      <option value="on">on</option>
    </select>
    <label>Slot 2</label>
    <select id="slot2" name="slot2">
      <option value="NULL">---Select---</option>
      <option value="on">on</option>
    </select>
    // ...
  </div>
</table>

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

jQuery select "change" event not fired


Tag : jquery , By : cnemelka
Date : March 29 2020, 07:55 AM
it fixes the issue Because the #second element just exists don't have to use the delegate and live functions
$('#first').change(function(){ 
  $.ajax( 
  { 
    'type':'GET', 
    'data':{'changed':$('#first').val()}, 
    'url':'myURL1', 
    'cache':false, 
    'success':function(html){
      $("#second").html(html);
     }
   }); 
   return false; 
});

$('#second').live("change", function()   
{   
  alert('test');   
}); 
$('#first').change(function(){ 
  $.ajax( 
  { 
    'type':'GET', 
    'data':{'changed':$('#first').val()}, 
    'url':'myURL1', 
    'cache':false, 
    'success':function(html){
      $("#second").html(html);
      $("#second").change();
     }
   }); 
   return false; 
});

jQuery change event fired twice when giving focus to another control inside the change callback


Tag : google-chrome , By : Willem van Schevikho
Date : March 29 2020, 07:55 AM
I hope this helps . The focus() on other control in your change eventhandler call the change event in chrome because it unfocus "blur" your current control if the value is different.
This bug is not new, you can take a look at this bug ticket on jQuery : http://bugs.jquery.com/ticket/9335
$('#foo').change(changeHandler);

function changeHandler() {
    console.log('Changed!');
    $(this).off('change').blur().on('change', changeHandler);
    $('#bar').focus();
}

Backbone change event not fired for select field selected by jQuery


Tag : javascript , By : BinaryBoy
Date : March 29 2020, 07:55 AM
Does that help I have a button that when clicked sets the value of a select field and also have a change event for the select field. The 'change select[name=location_id]' event works when selecting normally via mouse/keyboard but clicking the button, which uses jQuery to change the select field value, doesn't fire off the 'change select[name=location_id]' event. , All you need is to add an event:
this.$('select[name=location_id]').val(this.model.get('hd_location_id')).trigger('change');

ajax 'change' is not fired when value 'select value is set from php


Tag : javascript , By : Chandra P Singh
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a form with city, state and country. the upon selecting a city - I automatically populate the state and country via ajax from the a mysql database. , Try to replace your javascript code to:
 function profileLocationFunction() {
      var id = $(this).val();
      var dataString = id;
      $.ajax({
        type: "GET",
        url: "locationautocomplete.php",
        dataType: 'json',
        data: {
          datatext: dataString,
          type: 'cityselect'
        },
        cache: false,
        success: function(data) {
          $('#profilestate').html(data.message1);
          $('#profilecountry').html(data.message2);
        },
        error: function(xhr, status, error) {
          alert("Hello! ERROR!" + error);
        }
      });

    }


$(document).ready(function() {
       $('#profilecity').change(profileLocationFunction).change();

});

Jquery change event not fired when using selected attribute on select item


Tag : javascript , By : qba73
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • XPath select on child nodes
  • Idle event on mouse move - how to stop script permanently on mouse move
  • Why do I get logic is not defined in this code
  • When I enter a number in the input, the first function starts
  • ES6 imports and node_modules hell
  • How to get css selector of clicked-on element?
  • How to auto input MySQL data (input column 1 data to auto input column 2 data with same column) in php?
  • Appending to Datalist Via Javascript
  • Customizing Material-UI Components with Styled-Components
  • Populate elements with class name using Javascript
  • How to execute promises in batches (first resolving 10, then next 10 and so on)
  • Maximo JS automation script: "importPackage" is not defined
  • How to surround selected text inside textarea with <em> on key combination CTRL+i?
  • Your configured registry does not support audit requests ~ On npm audit
  • What's the Angular equivalent of Vue <slot/>?
  • Iconify icon not updating when "data-icon" set with setAttribute()
  • Javascript newbie stuck with XMLHttpRequest return data
  • How to add obj to to state in my example Redux React
  • Loading "grunt-karma.js" tasks...ERROR >> SyntaxError: Unexpected identifier
  • Why do I get log is not defined
  • get the multiple index of the same string
  • How can I iterate over an Array object by using For IN or FOR OF
  • Launch local executable from web browser
  • How to prepare array of json objects to d3 hierarchical tree structure
  • VueJS - event emitted - show HTML before other calculations
  • Export SASS/SCSS variables to Javascript without exporting them to CSS
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Make web service not accessible by browser?
  • Select DOM elements within template
  • How to customize angular ui-grid row background color?
  • Binding data in two respective $scope in AngularJS, how?
  • react put some props after import class
  • Variable is distributed among same components
  • switch class in jQuery/JavaScript for active/inactive state
  • How To Track Outbound Clicks Using Javascript
  • How to get total seconds elapsed since the beginning of the month?
  • Protractor: Stale element reference at "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"
  • Access to an array inside an object
  • Reseting an JavaScript Interval with different value?
  • Learning Angular for the first time - version 2 or 4?
  • Rendering RSS feed (XML) in React with jquery, why do object values parsed from the feed seem empty?
  • Node.js xml2js http.request tag matching
  • applying texture to custom plane geometry in Threejs
  • How do I pass an Event as a function parameter in HTML written inside JavaScript variable?
  • Interface of object parameter with default value and default property
  • Does using try / catch rather than `.catch` observable operator with XHR requests reduce performance?
  • In IE11 background colors not working on printing
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com