javascript - Can't select element's value loaded by PHP from Database -
i have enlisted data database using php. i'm unable select element's value. although data loaded in dom. if write html markup i'm able select other
<?php $results = $mysqli->query("select product_code, product_name, product_desc, product_img_name, price products order id asc"); if($results){ $products_item = '<ul class="products">'; //fetch results set object , output html while($obj = $results->fetch_object()) { $products_item .= <<<eot <li class="product"> <form method="post" onsubmit="return false"> <div class="product-content"><h3>{$obj->product_name}</h3> <div class="product-thumb"><img src="images/{$obj->product_img_name}"></div> <div class="product-desc">{$obj->product_desc}</div> <div class="product-info"> price {$currency}{$obj->price} <fieldset> <label> <span>color</span> <select name="product_color" class='product_color'> <option value="black">black</option> <option value="silver">silver</option> </select> </label> <label> <span>quantity</span> <input type="text" size="2" maxlength="2" name="product_qty" class='product_qty' id='product_qty' value="1" /> </label> </fieldset> <input type="hidden" name="product_code" value="{$obj->product_code}" /> <input type="hidden" name="type" value="add" /> <input type="hidden" name="return_url" value="{$current_url}" /> <div align="center"><button class="add_to_cart" id='add_to_cart' type='submit'>add</button></div> </div></div> </form> </li> eot; } $products_item .= '</ul>'; echo $products_item; } ?>
here's javascript im using.
window.onload = function () { $('.add_to_cart').click(function(){ $parent = $(this).parent(); $qty = $parent.children('.product-desc').text(); alert($qty); }) };
above code works, if use html markup rather using above html generated using php.
thanks in advance. searched lot didn't find relevant. although tried javascripts '.on' , '.live' function didn't worked.
$parent = $(this).parent();
selects <div align="center">
around button. use $parent = $(this).closest('.product');
instead.
though recommend binding submit
event of form instead of click
of button.
Comments
Post a Comment