Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    (newb) Why is my alert popping up on EVERY load ??

    I need to throw an alert conditionally, but it's throwing every time.
    This is my first attempt at jQuery, and I'm excited to finally be playing with it.
    (Every year I promise myself I'll get more familiar with JS, and I have yet to follow through ... so I'm trying to do something about it :-)

    I'm throwing together a page where contenteditable=true if a validated key gets passed.
    Here's the basics of my simple PHP:
    PHP Code:
    if(!empty($_GET['editkey'])){
        
    $secretOfTime $_GET['editkey'];
        if(
    $secretOfTime == md5(round($timestamp, -1)) || $secretOfTime == md5(round(($timestamp-3), -1))){
            
    //match
            
    $editable 'contenteditable="true"';
            
    $notValidated '';
        }else{
            
    //no match
            
    $editable '';
            
    $notValidated '<span id="editModeFailedToValidate"></span>';
        }

    And here's my JS:
    Code:
    <script type="text/javascript">
        $(document.getElementById('editModeFailedToValidate')).ready(function() {
                alert('The Edit-Key did not match, Please try again.');
        });
    </script>
    It throws the alert regardless of whether the 'editModeFailedToValidate' ID is found.

    I even tested with getElementById('someNonsenseTestValue'), and it still gave the alert.

    Am I going about this all wrong, or do I just need some little tweaks?

    ~ Mo
    Last edited by mOrloff; 11-02-2011 at 06:11 PM.
    ...because it's dundant already.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    <script type="text/javascript">
        $(document).ready(function() {
    if ($("#editModeFailedToValidate")){
                alert('The Edit-Key did not match, Please try again.');
    }
        });
    </script>
    Last edited by DanInMa; 11-02-2011 at 06:23 PM. Reason: forgot the hash

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Very cool, thank you.
    So, can I ALWAYS use the hash in place of getElementBtId ??
    That's a nice little shortcut.

    ~ Mo
    ...because it's dundant already.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,636
    Thanks
    6
    Thanked 1,003 Times in 976 Posts
    Quote Originally Posted by mOrloff View Post
    So, can I ALWAYS use the hash in place of getElementBtId ??
    That's a nice little shortcut.
    Haha, that kinda makes me laugh because in my perception this is the first thing one notices about jQuery. You can use any CSS selector in the dollar function to address elements, and even more that aren’t part of CSS:
    http://api.jquery.com/category/selectors/

  • #5
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    It's still throwing the alert regardless.
    Even when I try if ($("#someNonsenseTestValue")).

    I did a hard-refresh to make sure it's not a caching issue, and verified in the page source that your improved code is in fact what's being used.

    Other thoughts??
    ~ Mo
    ...because it's dundant already.

  • #6
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by VIPStephan View Post
    Haha, that kinda makes me laugh ...
    I do what I can to make people happy
    ~
    ...because it's dundant already.

  • #7
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,951
    Thanks
    9
    Thanked 724 Times in 718 Posts
    Try:

    Code:
    <script type="text/javascript">
        $(document).ready(function() {
    if ($("#editModeFailedToValidate").length>0){
                alert('The Edit-Key did not match, Please try again.');
    }
        });
    </script>
    I think I'm right in saying jQuery always returns a selector object regardless of whether a match is found - so if($("#editModeFailedToValidate")) will always return true. Adding the .length property will evaluate the object and its result can then be tested.
    Last edited by SB65; 11-02-2011 at 06:59 PM.

  • #8
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Excellent!!
    That did it.
    Thanks.

    ~ Mo
    ...because it's dundant already.

  • #9
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    ok im embarrassed. I could have sworn you didnt have to do it by length, but I was wrong.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •