0

Hi I can access the values of these buttons

for($i = 0; $i < 10; $i++){
$newArray[] = "<input type='button' name='$i' value='($i)' class='najam' id='$i' onclick='myFunction();' />";
}

myFunction

 function myFunction(){
// here to get the values of the buttons
 }

3 Answers 3

4

Change the PHP to pass the element:

for($i = 0; $i < 10; $i++){
$newArray[] = "<input type='button' name='$i' value='($i)' class='najam' id='$i' onclick='myFunction(this);' />";
}

and do

function myFunction(elem){
    alert(elem.value);
}

or just use a proper event handler

$('.najam').on('click', function() {
    alert(this.value);
});
Sign up to request clarification or add additional context in comments.

Comments

0

on the button, set the onClick or even to myFunction({$i})

in in your javascript:

function myfunction(id){
    //use id
}

Comments

0

Simply use Jquery.

$(".najam") // this is the selector for all items with the class "najam"

And then you can use it with methods like .click(), .each(), etc.

1 Comment

Other than linking the jQuery home page, it may be worth to show how to use it to solve the OP problem.

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.