javascriptdork Posted February 15, 2011 Share Posted February 15, 2011 I create a createElement("div") on every row of the array, and on that new div listed under the variable NEW_DIV. I need a different onClick effect. How would I do this? Quote Link to comment https://forums.phpfreaks.com/topic/227693-dynamic-onclick/ Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 Not sure exactly what you mean, but I'll go by what I think you mean... When you create the <div> that needs the clickable effect, give the div an id. Then either document.getElementById or a jQuery selector will get you that element to attach the clickable event to. Personally,I'm a big fan of jQuery: //create the div $('<div id="clickableDiv">Content of Div here</div>').appendTo('#originalDiv'); $('#clickableDiv').click(function(){ your onClick action goes here ... }; This assumes the containing div has id=originalDiv and the dynamically created div gets the id "clickableDiv" as shown in the code. In jQuery appendTo will put it at the end of originalDiv; alternately you can use prependTo() to put it at the beginning and push everything else down or to the side (depending on your css). If the div is empty, it doesn't matter which one you use, really. If an entire array is creating the clicable divs, you can dynamically create id's using the index pointer during the foreach (or .each() if you're using jQuery) Quote Link to comment https://forums.phpfreaks.com/topic/227693-dynamic-onclick/#findComment-1175981 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.