Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, I think I understand the issue now.</p> <p>Every time you do a focus on the <code>product_calendar</code> elements, you do a <code>fadeIn</code> on the <code>#calendar_addRemove</code> element. Every time you do that <code>fadeIn</code>, you use its callback to bind <strong>new</strong> blur handlers to the <code>calendar_add</code> and <code>calendar_remove</code> elements. That means that over time, those elements will have <strong>multiple</strong> blur handlers (all executing the same logic.) That can't be what you want.</p> <p>In the script below, I've pulled out the nested handlers so that they're only bound once to each element. Note that:</p> <ul> <li><p><code>product_calendar</code> is declared (as <code>null</code>) at the top of the anonymous function, and then updated by the focus handler on the <code>product_calendar</code> element. I <em>think</em> this results in correct behavior.</p></li> <li><p><code>attr_val</code> is declared and assigned locally in both of the blur handlers. Again, I <strong>think</strong> this results in the correct behavior: If you were to declare it <em>outside</em> of the blur handlers (as <code>product_calendar</code> is declared), then you might accidentally use old values when you access it.</p></li> </ul> <p>I'm still not sure exactly how this code is supposed to function, but this script performs in a way that I'd consider "reasonable".</p> <p>(By the way, production code should probably allow for whitespace at the beginning and end of the input strings.)</p> <pre><code> $(function() { var product_calendar = null; $('input[name=product_calendar]').css({ 'color': '#5fd27d', 'cursor': 'pointer' }).attr({ 'readonly': 'readonly' }); // Additional formatting for specified fields $('input[name=calendar_add]').blur(function() { // After value entered, action occurs on blur alert('Blur'); // Added for testing var add_val = $('input[name=calendar_add]').attr('value'); if (add_val != '') { alert('Not Blank'); // Added for testing var attr_val = $(product_calendar).attr('value'); var newAdd_val = attr_val + ' ' + add_val; $(product_calendar).attr({ 'value': newAdd_val }); $('#calendar_addRemove').fadeOut(500); } else { alert('Blank'); // Added for testing $('#calendar_addRemove').fadeOut(500); } }); $('input[name=calendar_remove]').blur(function() { // After value entered, action occurs on blur alert('Blur'); // Added for testing var remove_val = $(this).attr('value'); if (remove_val != '') { alert('Not Blank'); // Added for testing var attr_val = $(product_calendar).attr('value'); if (attr_val.indexOf(remove_val) != -1) { alert('Eval True'); // Added for testing var newRemove_val = attr_val.replace(remove_val, ''); $(product_calendar).attr({ 'value': newRemove_val }); $('#calendar_addRemove').fadeOut(500); } else { alert('Eval False'); // Added for testing $('#calendar_remove').after('&lt;p class="error"&gt;Occurrence Not Found&lt;/p&gt;'); $('.error').fadeOut(1500, function() { $(this).remove(); }); } } else { alert('Blank'); // Added for testing $('#calendar_addRemove').fadeOut(500); } }); $('input[name=product_calendar]').focus(function() { // Focus on any 'input[name=product_calendar]' executes function product_calendar = $(this); $('#calendar_addRemove input').attr({ 'value': '' }); // Clear input fields $('#calendar_addRemove').fadeIn(500); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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