/    Sign up×
Community /Pin to ProfileBookmark

set or assign table id dynamically using javascript

Hello Everyone,

I would like to dynamically set the ID of a table using javascript and then alert it. Does anyone know how to do this?

Thanks in advance. ?


————————————–

<html>
<head>
<script type=”text/javascript”>
function setTableID()
{
document.getElementsByTagName(“table”).id=”tableid” //–does not work.
}
</script>
</head>

<body>
<table border=”1″>

</table>
</body>

</html>


—————————-

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423May 01.2006 — try this..

document.getElementsByTagName("table")[0].setAttribute("id","tableid")


document.getElementsByTagName returns an array
Copy linkTweet thisAlerts:
@KorMay 01.2006 — and you need an event to trigger the function. It might be onload, for instance:
[code=php]
<script type="text/javascript">
function setTableID()
{
document.getElementsByTagName("table")[0].setAttribute("id","tableid");
}
onload =setTableID;
</script>
[/code]
Copy linkTweet thisAlerts:
@napoleonvauthorMay 02.2006 — This works.
Copy linkTweet thisAlerts:
@felgallMay 02.2006 — Perhaps like this:

&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
function deleteRow(i)
{
i.deleteRow(i.rowIndex)
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;Row 1&lt;/td&gt;
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode)"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Row 2&lt;/td&gt;
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode)"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Row 3&lt;/td&gt;
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode)"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;

&lt;/html&gt;
Copy linkTweet thisAlerts:
@Orc_ScorcherMay 02.2006 — <i>
</i>function deleteRow(input) {
var tr = input.parentNode.parentNode
tr.parentNode.removeChild(tr)
}



&lt;tr&gt;
&lt;td&gt;Row 1&lt;/td&gt;
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this)"&gt;&lt;/td&gt;
&lt;/tr&gt;
Copy linkTweet thisAlerts:
@napoleonvauthorMay 02.2006 — Thanks but this one does not work. To try it, copy the source, go to:

http://www.w3schools.com/js/tryit.asp?filename=tryjs_text

and paste.
Copy linkTweet thisAlerts:
@napoleonvauthorMay 02.2006 — try this..

document.getElementsByTagName("table")[0].setAttribute("id","tableid")


document.getElementsByTagName returns an array[/QUOTE]



This works great! THANKS! ?

Here is my code:


------------------------

<html>

<head>

<script type="text/javascript">

function deleteRow(i)

{

document.getElementsByTagName("table")[0].setAttribute("id","tableid")

document.getElementById("tableid").deleteRow(i)

}

</script>

</head>

<body>

<table border="1">

<tr>

<td>Row 1</td>

<td><input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"></td>

</tr>

<tr>

<td>Row 2</td>

<td><input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"></td>

</tr>

<tr>

<td>Row 3</td>

<td><input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"></td>

</tr>

</table>

</body>

</html>


------------------------
×

Success!

Help @napoleonv spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...