javascript - Jquery each function target children of div -


i trying target buttons, children of div class 'actions'.

i want function take values onclick, filter out letters , special chars, , add numbers array.

the function works, when try target buttons outside div, not when try target .actions button.

anyone have ideas?

fiddle: http://jsfiddle.net/teilmann/wn79n/5/

html:

<div class="actions">     <button onclick="addtocart(1337, 'something');" /> </div> <div class="actions">     <button onclick="addtocart(1338, 'something');" /> </div> <div class="actions">     <button onclick="addtocart(1339, 'something');" /> </div> 

js:

var products = new array();  jquery('.actions button').each(function(){     var id = jquery(this).getattribute('onclick');     products.push(id.replace(/[^0-9]/g, '')); });  alert(products); 

your html incorrect <button/> needs <button></button> , getattribute has .attr()

html

<button onclick="str1"  ></button> <!--changed html here <button/>--> <button onclick="2" ></button> <!--changed html here <button/>--> <button onclick="addtocart(1234, 'something');"  ></button> <!--changed html here <button/>-->  <div class="actions">     <button onclick="addtocart(1337, 'something');" /> </div> <div class="actions">     <button onclick="addtocart(1338, 'something');" /> </div> <div class="actions">     <button onclick="addtocart(1339, 'something');" /> </div> 

jquery

var products = new array();   jquery('.actions button').each(function(){     var id = jquery(this).attr('onclick');     products.push(id.replace(/[^0-9]/g, '')); });  alert(products); 

demo

if don't close button </button> html automatically generate markup

<button onclick="str1"></button> <button onclick="2"></button> <button onclick="addtocart(1234, 'something');">     <div class="actions"></div> </button> <button onclick="addtocart(1337, 'something');">     <div class="actions"></div> </button> <button onclick="addtocart(1338, 'something');">     <div class="actions"></div> </button> <button onclick="addtocart(1339, 'something');"></button> 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -