#1
  1. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,435
    Rep Power
    24

    if/else not detecting correctly?


    Simply said it should change from one to the other. However, the original text is 'Edit Icons', but the initial process comes out as false, running the 'else' which changes it back to 'Edit Icons' as it starts. The second time through recognizes it correctly and starts to change it back and forth as intended from there. Anyone see reason why the initial run does not come out as TRUE?
    HTML
    Code:
    <TD>Edit Icons</TD>
    JavaScript (jQuery)
    Code:
    if($(this).text() == 'Edit Icons') {
      $(this).text('Save | Cancel');
    } else {
      $(this).text('Edit Icons');
    }
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,094
    Rep Power
    9644
    Well, what's the actual value of .text() the first time? console.log that. Actually, JSON.stringify + console.log that.
  4. #3
  5. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,435
    Rep Power
    24
    Thank you for the 'log' suggestion. Apparently since I was writing my PHP function's loop in shorthand to end lines as below, the newline was being defined within, instead of after the intended/assumed closing tag.
    PHP Code:
    '<TD class="action">Edit Icons' "\n"
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  6. #4
  7. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,094
    Rep Power
    9644
    I thought it might be something like that.

    Use a class. They aren't just for CSS... I say, though I'm about to use some anyways:
    Code:
    <td class="editing-container">
      <span class="not-when-editing">Edit Icons</span>
      <span class="when-editing">Save | Cancel</span>
    </td>
    Code:
    .editing-container:not(.editing) .when-editing,
    .editing-container.editing .not-when-editing { display: none; }
    Code:
    $(this /* being the td */).is(".editing") // test for state
    
    $(this).toggleClass("editing") // toggle state
    The idea is to have everything present on the page from the start, then use classes to track state and some CSS to manage visibility.
    Last edited by requinix; June 18th, 2017 at 03:33 PM.
  8. #5
  9. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,435
    Rep Power
    24
    Nice idea. Thanks for the input.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.

IMN logo majestic logo threadwatch logo seochat tools logo