Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-02-2011, 05:57 PM   PM User | #1
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
(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
__________________
...because it's dundant already.

Last edited by mOrloff; 11-02-2011 at 06:11 PM..
mOrloff is offline   Reply With Quote
Old 11-02-2011, 06:18 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
Code:
<script type="text/javascript">
    $(document).ready(function() {
if ($("#editModeFailedToValidate")){
            alert('The Edit-Key did not match, Please try again.');
}
    });
</script>
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 11-02-2011 at 06:23 PM.. Reason: forgot the hash
DanInMa is offline   Reply With Quote
Old 11-02-2011, 06:39 PM   PM User | #3
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
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.
mOrloff is offline   Reply With Quote
Old 11-02-2011, 06:46 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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/
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 11-02-2011, 06:47 PM   PM User | #5
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
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.
mOrloff is offline   Reply With Quote
Old 11-02-2011, 06:49 PM   PM User | #6
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
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.
mOrloff is offline   Reply With Quote
Old 11-02-2011, 06:56 PM   PM User | #7
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
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..
SB65 is offline   Reply With Quote
Old 11-02-2011, 07:03 PM   PM User | #8
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
Excellent!!
That did it.
Thanks.

~ Mo
__________________
...because it's dundant already.
mOrloff is offline   Reply With Quote
Old 11-02-2011, 07:15 PM   PM User | #9
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
ok im embarrassed. I could have sworn you didnt have to do it by length, but I was wrong.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:24 AM.


Advertisement
Log in to turn off these ads.