I created a dynamic button,and used ajax to call it when clicked. when each of the button is clicked it fetches a list of items from a website, which makes it dynamic.
foreach($items as $item_link){
$i++;
$item_name = $name[$i];
<button type="button" id="btn" name="btn" value="'.$item_link[$i].'" onclick= " getItem(this.value);">'.$item_name.'</button>
}
I noticed that when each of the buttons are clicked it only return the same value,instead of dynamically returning different values of the button. My question is: what could be the cause? Although the button is not in a form, could that also be a reason?
ID's can cause multiple problems but I don't see where you attempt to append this button. You haven't even stored it into a variable so as your source code stands it will throw a syntax error. I suggest you format the source code in your question so it doesn't mislead anyone into thinking you are trying to execute the script as it looks in your question.id="btn"toclass="btn", becauseidneed to be unique per element.2.What data you are fetching on button click(add that code so that we can check). 3. Add the HTML part also for more clarity