0

I want to create new buttons using a single button, and after creating those new buttons, I want to click on them and have their id number sent to the php file.

my code:

<script>
var count=1;

   document.getElementById("btn").onclick=function(){
      document.getElementById("col").innerHTML+="<tr><td><button type='submit' name='id' id='clickable'>"+count+"</button></td></tr>";
      var cid=count;
      $("#clickable").click(function(){
         location.href="trial29.php?id="+cid;
      });
      count++;
   }

</script>

The problem is that I'm not able to make more than one button clickable. Also I'm not able to set the id of the button to the value they have on them.

5
  • 2
    id is a unique identifier. Having a lot of items with same id means that only first one will be considered. Commented Jan 29, 2017 at 9:32
  • you should use 'on('click',...' as you create those buttons on the fly. Consider adding a jsfiddle or describe the error more specifically. Commented Jan 29, 2017 at 9:33
  • use a common class instead of id Commented Jan 29, 2017 at 9:52
  • thanks! yes using a class worked Commented Jan 29, 2017 at 10:32
  • $("#clickable").click(function(){ location.href="trial29.php?id="+cid; }); this block have problem and caused the code to stop that is why the count remain equal 1 and did not increased Commented Jan 29, 2017 at 11:45

1 Answer 1

0

use this code ...

<script>
var count=1;

document.getElementById("btn").onclick = function(){

    document.getElementById("col").innerHTML+="<tr><td><button type='submit' name='id_"+count+"' class='newbtn' id='"+count+"'>"+count+"</button></td></tr>";
    var cid=count;
    count++;
    alert(count)

}

    $(document).on("click", ".newbtn", function(e) {
        var cid = $(this).attr('id');

        alert(cid);
        location.href="trial29.php?id="+cid;
    });

</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.