Use script dynamically

Hi!
Look.
I have such sequence
A few divs with different names which are generated automatically

<div id=“<?php echo $oneid;>”>

then I have inside each this div input fields and
button with next attributes

<a id=“buttonid” class=“button”><span><?php echo $button; ?></span></a>
</div>

And below I have javascript which is applying only to one certain div class with name active so I have to make div with any id work when button was pressed.

<script type=“text/javascript”><!–
$(‘#buttonid’).bind(‘click’, function() {
$.ajax({
url: ‘index.php?route=path/to/update’,
type: ‘post’,
data: $(‘.active input[type=\‘text\’], .active input[type=\‘hidden\’], .active input[type=\‘radio\’]:checked, .active input[type=\‘checkbox\’]:checked, .active select, .active textarea’) …

This is the question how to make work this script after user press any $button?
When on page is only one div it works fine.
Next way doesnt work
<a onclick=“document.getElementById(‘<?php echo $oneid;?>’).setAttribute(‘class’, ‘active’);” id=“buttonid” class=“button”><span><?php echo $button; ?></span></a>

Any ideas?

Your javascript (which is JQuery, by the way) applies to an ID of “buttonid”.
A page can only have one instance of a CSS ID (an ID must be unique).
That answers your question.

If you want to apply this to all buttons, I suggest you define them with the same unique CSS class and modify your JQuery to ‘bind’ to that class. In that case, avoid applying a CSS ID to the button(s) in your PHP output because you will have the same ID duplicated on the page.

Wow Parkin its a point. I passed by it without notice. I dont know how I will reorganize my code but after it I promise to write here.

Can you help?
This doesnt work

<a onclick="document.getElementById('<?php echo $oneid;?>').setAttribute('class', 'active'); this.setAttribute('id', 'buttonid');"  class="button"><span><?php echo $button; ?></span></a>

Abandon the idea of using an ID altogether. And modify your original code like this:


$('.buttonstyle').bind('click', function() {
...

This assumes you have a CSS class “buttonstyle” defined {you can name it as you please}

Works fine as you’ve advised. Respect to you Parkin.
I plan to learn javascript thoroughtly.