2

i have a div where i am running a for loop. Which is creating the below divs based on the number of rows in the fields.

<div class="parent">
                <p style=" font-family: Times New Roman;color:#a0a0a0; ">
                        Content 1 goes here.........
                </p>
                <div class="child">
                        <span> <a class="likebtn">Like</a> </span>1 people like this     
                </div>
            <hr/>
 </div> 

the above shown div is in a for-each loop i.e creating multiple divs same like this with different content on it.

Now i am trying to call my jquery function , if users click on any like button residing into the div. I tried the below function but my function is not getting call. Please help me , why regarding this issue.

<script type="text/javascript">         
        $('.parent .child .likebtn').click(function(e)
        { 
            alert("function called");
        });
</script>

And one thing,div's are created dynamically, if i write my above javascript code just after my for-each loop then my function is getting called. It's not good practice to write javascript function in Controller. I am using COdeigniter framework. Plase help me, i have to specify this function either in head or body

7
  • Is it dynamicly created by php or jQuery? The for-each term is confusing me. Commented May 10, 2013 at 14:52
  • @Karl-AndréGagnon Yes it is dynamically creating by php in my controller class I am using codeigniter framework. I added few points in my question above. Please read that. Commented May 10, 2013 at 14:53
  • May be parent is a reserved word....try to change the name of class parent to parento ...just for debugging Commented May 10, 2013 at 14:55
  • When the user click on LIKE, nothing happen? Commented May 10, 2013 at 14:56
  • @Ashutosh jQuery doesn't care about how your server is structured, what karl meant was is the client being dynamically updated after the page has loaded with new content. My guess is yes, due to infinite scroll plugin. tymeJV's answer should fix it. Commented May 10, 2013 at 14:56

2 Answers 2

5

If the div's are created dynamically, call them like

$(document).on('click', '.parent .child .likebtn', function(e)
    { 
        alert("function called");
    });
Sign up to request clarification or add additional context in comments.

5 Comments

Yes now its working. My divs are created dynamically i forget to say that. I want to know, why mention your function in this way if divs are created dynamically?
You can't assign a click handler to an element that doesn't exist yet (your elements aren't there on DOM ready). Look into "event delegation", this will help :)
To add to tymeJV's comment, using the syntax in this answer actually binds the event to the document, which DOES exist, compared to the elements that don't exist yet that will trigger this event.
@tymeJV if you have time, can you explain it in little details, still i am half understood!
I would refer you to the API for this: api.jquery.com/on -- Has all the info you need.
1

Wrap your function in enclosure to make sure it gets executed after your DOM has fully loaded like so:

$(function() {
    $('.parent .child .likebtn').click(function(e) { 
        alert("function called");
    });
});

EDIT: Are you loading this content from AJAX? In this case you need to use the .on() function to make sure new dom elements get the events, syntax somethign like this: $(document).on(events, selector, data, handler); -> api.jquery.com/on

EDIT 2: Same answer as tymeJV

3 Comments

He doesn't need to since it's before the closing body tag.
I tried but its not getting called. And one thing, if i write my above javascript code just after my for-each loop then my function is getting called. It's not good practice to write javascript function in Controller. I am using COdeigniter framework. Plase help me, i have to specify this function either in head or body
Are you loading this content from AJAX? In this case you need to use the .on() function to make sure new dom elements get the events, syntax somethign like this: $(document).on(events, selector, data, handler); -> api.jquery.com/on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.