1

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
asked Oct 3, 2015 at 15:37
0

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1
<!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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.