I am trying to figure out how to get the value of a button using JavaScript. There are several buttons generated using a while statement in php code, so the name
and id
are the same for all of them. The only difference between them is the value.
Part of my php code:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align=\"center\"><input type=\"button\" id=\"details\" name=\"details\" value=\"" . $row['mcn'] . "\" onClick=\"MCNdetails()\"></td>";
echo "</tr>";
}
What I'm trying to achieve is that when this button is clicked a JavaScript alert is displayed with this records details.
(FYI - "Faker" is the form name that this table is a part of.)
My JavaScript code:
function MCNdetails()
{
var buttonDetails = document.Faker.details;
var buttonValue = buttonDetails.value;
if (buttonValue === "2700-2057" )
{
alert ("Details are here");
return;
}
else
{
alert ("No Dice " + buttonValue);
return;
}
}
I have approx. 300 records so far. When I click on ANY of the buttons I always get the else statement. I did try getting the element by id like this:
function MCNdetails()
{
var buttonDetails = document.getElementById("details");
var buttonValue = buttonDetails.value;
if (buttonValue === "2700-2057" )
{
alert ("Details are here");
return;
}
else
{
alert ("No Dice " + buttonValue);
return;
}
}
But that would always return the value of the first button regardless of which one I clicked.
I have done some Google searching as well as searching this site and I can't find this same scenario where the buttons are generated.
Any help or pointers would be appreciated. Thank you for your time :)