I have <td> with an Id <td id="firstApp">. I have write onclick event for button. when button is clicked showDate function iscalled. I want to pass 'id' as parameter to this showDate function so my function will get to know on which id it should act.
I tried,
<td><button type="button" onclick='document.getElementById("firstApp").innerHTML=Date()'>
Then it works fine.
But when I pass id as function call,
<td><button type="button" onclick='showDate("firstApp")'>
Then it won't work, can anybody tell me where I went wrong...!!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<tr>
<td>App 1</td>
<td><button type="button" onclick='showDate("firstApp")' name="app1" >on/off</td>
<td>10</td>
<td id="firstApp"></td>
<td></td>
</tr>
<!-- <button type="button">Update</button> <button type="button">Log Out</button> -->
</table>
</div>
<script>
//write script herei
function showDate(appId)
{
document.getElementById("appId").innerHTML = Date();
}
</script>
</body>
</html>
Artjom B.
62k26 gold badges137 silver badges236 bronze badges
2 Answers 2
You don't want to put quotes around the argument when using it:
function showDate(appId) {
document.getElementById(appId).innerHTML = Date();
// No quotes here ------^----^
}
answered Oct 3, 2015 at 15:49
T.J. Crowder
1.1m201 gold badges2k silver badges2k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<table>
<tr>
<td>App 1</td>
<td><button type="button" onclick='showDate("firstApp")' name="app1" >on/off</td>
<td>10</td>
<td id="firstApp"></td>
<td></td>
</tr>
<!-- <button type="button">Update</button> <button type="button">Log Out</button> -->
</table>
</div>
<script>
//write script herei
function showDate(appId)
{
document.getElementById(appId).innerHTML = Date();
}
</script>
</body>
</html>
Remove quote from appId
document.getElementById(appId).innerHTML = Date();
answered Oct 3, 2015 at 15:52
Md Rashedul Hoque Bhuiyan
10.7k5 gold badges33 silver badges42 bronze badges
Comments
default