0

I am trying to achieve code reusability by whenever a div is clicked, it gives out the div's numberDiv attribute. It works on the first default div that has a class divClassSelector, but I have some other js code that whenever you press the button "+1 Div", inserts another div. Then, when I try to alert the newly created div's numberDiv attribute, it does not work. I am assuming that it is because the newly created div did not exist in the DOM when the page was first loaded, therefore it cannot find the new div. I could be wrong though.

Does anybody know a workaround to this issue? Any help is much appreciated.

Here's my code:

//HTML
...
<body>
    <div class="divClassSelector" numberDiv="1"></div>
    <button id="plusOneDiv">+1 Div</button>
</body>
...

//My JS:

//Code to alert the div's numberDiv attribute value
$(".divClassSelector").on("click", function(){
    var numberDiv = $(this).attr("numberDiv");
    alert(numberDiv);
});

//Code to insert another div
var count = 2;
$("#plusOneDiv").click(function(){
    $(body).append(<div class="divClassSelector" numberDiv="'+count+'">');
    count++;
});

Thank you for your help in advance and cheers!


UPDATE 1:

Does anyone know how to instead on clicking the div, trigger an event that is triggered by having the "focus" on a form input field, that is, when you click on it and are interacting with it. I tried by using the "focus" method, but it kept alerting the same thing over and over, until I closed the tab, then I could escape from the alert infinite loop. Thanks!

4
  • use data-div-no="count" for div attribute its html standard Commented Mar 31, 2015 at 5:25
  • you can also use $(body).delegate('.className', 'click', function() {//your code goes here}); or follow this link api.jquery.com/delegate Commented Mar 31, 2015 at 5:29
  • check for console you might have error in your code. probably $(body).append(<div class="divClassSelector" numberDiv="'+count+'">'); you are missing '<div Commented Mar 31, 2015 at 5:31
  • 1
    Don't use alert() inside a focus() handler. Your focus handler fires, which pops up the alert, which fires the blur() event. You close the alert, which fires the focus() event, which pops up the alert, and you have an infinite loop. If you simply avoid using an alert(), binding events to focus() will work fine. Commented Mar 31, 2015 at 5:33

2 Answers 2

1

First of all add this '$' to your code: --$(this).attr("numberDiv");--

$(".divClassSelector").on("click", function(){
var numberDiv = $(this).attr("numberDiv");
alert(numberDiv);
});

Also change your click function to

$(document).on("click",".divClassSelector", function(){
var numberDiv = $(this).attr("numberDiv");
alert(numberDiv);
});
Sign up to request clarification or add additional context in comments.

10 Comments

I have it there, actually I forgot to put it. Still doesnt work
ok , then use this click function , in replacement to that.
.on(".divClassSelector","click", suppose to be .on("click",".divClassSelector",
Thank you it did work. Could you elaborate on the Update 1 of my post if possible? Thanks!
Yes i know that one. And selector that you make above is .delegate() way, not the .on() way. New comers will confuses, and always use new version of code since the older will deprecated.
|
0
$(document).on('click', '.divClassSelector', function(){
var numberDiv = $(this).attr("numberDiv");
alert(numberDiv);
});

or

$('body').on('click', '.divClassSelector', function () {...})

you forgot $

var numberDiv = (this).attr("numberDiv");

to

 var numberDiv = $(this).attr("numberDiv");

4 Comments

I have it there, actually I forgot to put it. Still doesnt work
did u try using $(document).on('click', '.divClassSelector', function(){
Thank you it did work. Could you elaborate on the Update 1 of my post if possible? Thanks!
@JackGal approve the answer then

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.