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 10-23-2012, 08:47 PM   PM User | #1
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
Question Detect anchor click

Hello, everyone.

I'm dynamically populating a div after the page loads with some anchor tags. Basically, click one or more names in a multi-select and the names are added with anchor tags around them so that they can be removed by clicking the name.

I would like to set it so that when each name is clicked, if an alert message in a particular div is blank, then the div will display "Changes to this record have not been saved."

I'm not getting the function right. I've tried several different ways, I'll include two of them below. These are all coded within the $(document).ready()

Code:
$('.remLink a').each(function(e){
  e.preventDefault();
  $(this).click(function(){
    $('#chgWarning').html("Changes to this record have not been saved");
  });
});
Code:
$('.remLink a').each(function(e){
  e.preventDefault();
  $(this).live('click',function(){
    $('#chgWarning').html("Changes to this record have not been saved");
  });
});
How does one apply an event to an anchor that doesn't exist when the page loads?

Thank you,
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-23-2012, 09:45 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,703
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
First of all, live() is deprecated as of jQuery 1.7, you should use on() if you’re using a recent version of jQuery.

Then, your problem is that each() loop which, as you correctly noted, doesn’t know about the non-existing anchors yet. Therefore, you need to add the click function/event to the function that creates/appends the links in the first place. Can you show us how the div is populated with the anchors?

Edit:
Oh, it might even be simpler: just don’t use each() but on() in the first place
PHP Code:
$('.remLink a').on('click', function(e){
  
e.preventDefault();
  $(
'#chgWarning').html("Changes to this record have not been saved");
}); 
But still, if you insist in using each() for whatever reason, you should add the click event when the anchors are created/appended as this each loop only accounts for the elements already existing.
__________________
Don’t click this link!

Last edited by VIPStephan; 10-23-2012 at 09:48 PM..
VIPStephan is offline   Reply With Quote
Old 10-23-2012, 10:03 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
Hi, VIPStephen.

I cannot copy/paste the code here because my dev system is isolated from the internet and I have NO way of transferring between this system and my dev. However, I will manually type most of the code.

Code:
<select id="developerName" name="developerName" multiple="multiple"> [ populated by database ] </select>
<input type="button" id="addDevelopers" name="addDevelopers" onclick="addDev(this);" /><div id="devSection"></div>
Code:
function addDev(btnObj){
  btnObj.blur();
  var v,t,thisHTML = $('#devSection').html();
  var devName = $('#developerName option:selected').map(
    function(){
      // checks to see if the name is already in the div - if yes, does nothing; if no, adds name
      return "<a href= blah blah blah blah blah > NAME </a>";
      }
    $('#devSection').html(
        $('#devSection').html("<br />" + devName + "<br />")
        );
    $('#devSection').html(
        // HERE I do a long process of split, sort, join, putting each name on a separate line and sorting alphabetically by surname
        );
    );
}
And that's it.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-23-2012, 10:07 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
I tried your suggestion (using on instead of each) and put it inside (at the end) of the function that inserts the anchors into the div. Still not firing, for some reason.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-24-2012, 12:01 AM   PM User | #5
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,703
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Quote:
Originally Posted by WolfShade View Post
I tried your suggestion (using on instead of each) and put it inside (at the end) of the function that inserts the anchors into the div. Still not firing, for some reason.
No, with the on() function in my edited part of the post I meant to just replace that with the current each loop set-up you have.

On the other hand: Is this where the links are created?
Code:
return "<a href= blah blah blah blah blah > NAME </a>";
If that’s the case – or generally – you should work with “regular” DOM methods rather than with innerHTML type methods. For example you can do:
PHP Code:
$('#devSection').append(
  $(
'<a>', {
    
hrefblabla,
    
textNAME,
    
click: function() {
      
// do whatever you wanna do on click
    
}
  })
); 
Using prepend() and append() you can not only add single elements to a container, you can also add document fragments you stored in a variable, e. g.:
PHP Code:
var fragment = $('<div>', {id'example'}).append($('<a>', {href'http://example.com'text'whatever'}));
$(
'#container').append(fragment); 
will result in
Code:
<div id="container">
  <div id="example">
    <a href="http://example.com">whatever</a>
  </div>
</div>
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
WolfShade (10-24-2012)
Old 10-24-2012, 01:27 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
I'll give that a shot. Thank you.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-24-2012, 02:43 PM   PM User | #7
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
I had to play around with it, a bit, but I finally got it to do what I want.

Thanks, again, VIPStephan!
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade 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 10:13 PM.


Advertisement
Log in to turn off these ads.