JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 27th, 2013, 05:48 PM
markjohnson markjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 6 markjohnson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 2 sec
Reputation Power: 0
jQuery - How to bind focus and blur on new elements

Hi, I am having trouble binding focus and blur to new elements.

I have the following HTML code:
Code:
    <div class="ticket">
        <input class="firstname" name="firstname[]" title="First Name(s)" value="" />
        <input class="lastname" name="lastname[]" title="Last Name" value="" />
        <a href="#"><img src="/images/admin/DeleteRed.png" /></a> OR
        <a href="#" class="add">Add more</a>
    </div>

I then have the following jquery code attached to 'Add more' hyperlink:

Code:
    $("body").on("click", "a.add", function() {
        var content = $(this).parent('.ticket').html();
        var newdiv = $("<div class='ticket'>");
        newdiv.html(content);
        $(this).parent('.ticket').after(newdiv);
        return false;
    });


As you can see, all it does is copy the current div and add it straight after.

The input fields within the div tag have the following jquery bound to it:

Code:
$('input:text').on({
    focus: function () {
        var self = $(this);
       
        if (self.val() == self.attr('title')) {
            self.val('');
        };
    },
    blur: function () {
        var self = $(this),
            val = jQuery.trim(self.val());
       
        if (val == "" || val == self.attr('title')) {
          self.val(self.attr('title')); 
        };
    }
}).trigger('blur');


It does seem to copy the div tag and place it right after the current one, however, it does not bind focus or blur to the newly added elements.

What am I doing wrong?

thanks

Reply With Quote
  #2  
Old February 27th, 2013, 11:04 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 16 h 17 m 48 sec
Reputation Power: 69
Is this what you were trying to do?
Code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    <div class="ticket">
        <input type="text" class="firstname" name="firstname[]" title="First Name(s)" value="" />
        <input type="text" class="lastname" name="lastname[]" title="Last Name" value="" />
        <a href="#"><img src="/images/admin/DeleteRed.png" /></a> OR
        <a href="#" class="add">Add more</a>
    </div>

<script>

    $("body").on("click", "a.add", function() {
        var content = $(this).parent('.ticket').html();
        var newdiv = $("<div class='ticket'>");
        newdiv.html(content);
        $(this).parent('.ticket').after(newdiv);
        return false;
    });

$(document).on('focus', 'input[type=text]', function() { 

        var self = $(this);
       
        if (self.val() == self.attr('title')) {
            self.val('');
        }

});

$(document).on('blur', 'input[type=text]', function() { 

        var self = $(this);
            val = jQuery.trim(self.val());
       
        if (val == "" || val == self.attr('title')) {
          self.val(self.attr('title')); 
        }

});

</script>

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > jQuery - How to bind focus and blur on new elements

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap