Right now I have multiple rows from mysql printing off the following in php:
while($row=mysql_fetch_array($result)){
echo "<input id='dd".$row["id"]."' onclick='myFunctions()' type='button' value='".$row["id"]."'></form>";}
I am able to retrieve only the most recent value of button dd.
function myFunctions() {
var name = document.getElementById("dd").value;
There are multiple rows though, so how can I get the value of the specific one that was clicked?
This is what the html looks like:
<input id="dd4199" onclick="myFunctions()" value="4199" type="button">
<input id="dd4198" onclick="myFunctions()" value="4198" type="button">
<input id="dd4197" onclick="myFunctions()" value="4197" type="button">
<input id="dd4196" onclick="myFunctions()" value="4196" type="button">
As you can see when it does getElementById it always finds the 4199 because it is the most recent. How can the respective click be found. Thanks!
You have to create inputs with different ids. It’s not correct to have the same id in 2 different controls.
You can iterate and create ids dd1, dd2, etc.
Check this: Can multiple different HTML elements have the same ID if they're different elements?
pass this
as an argument so when you click button you can get value of clicked button
function myFunctions(a) {
var name = a.value;
console.log(name);
}
<input onclick="myFunctions(this)" value="4199" type="button">
<input onclick="myFunctions(this)" value="4198" type="button">
<input onclick="myFunctions(this)" value="4197" type="button">
<input onclick="myFunctions(this)" value="4196" type="button">
</div>
function myFunctions(ele){
var element = ele.value;
alert(element);
}
<input onclick="myFunctions(this)" value="4219" type="button">
<input onclick="myFunctions(this)" value="5419" type="button">
</div>
//dont add inine function
//add a common class
<input class="getValue" value="4219" type="button">
<input class="getValue" value="5419" type="button">
//add event handler
for (var i = 0; i < getValue.length; i++) {
getValue[i].addEventListener('click', function(){
alert(this.vaue);
});
}
Try
for ($id = 0; $row = mysql_fetch_array($result); $id++) {
echo "<input id='dd_" . $id . "' onclick='select(" . $id . ")' type='button' value='".$row["id"]."'></form>";
}
Maybe generates:
<input id="dd_0" onclick="select(0)" value="4199" type="button">
<input id="dd_1" onclick="select(1)" value="4198" type="button">
<input id="dd_2" onclick="select(2)" value="4197" type="button">
<input id="dd_3" onclick="select(3)" value="4196" type="button">
Note: the id's are unique, so you can select them individually.
function select(id) {
var name = document.getElementById("dd_" + id).value;
}
Also, just a tip, your variable names should be more descriptive.
No one knows what dd means.
Good luck!