473,473 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to insert a new row to an existing HTML table

The following code (HTML) generates a table. Now I'd like to insert a new
row by a javascript.
The following code (javascript) works with the Internet Explorer and also
with Mozilla. However, the inserted button (onClick) in the table does not
work with the Internet Explorer. It only works with Mozilla.
I'm trying the whole Sunday without any success.

Please give me a hint.
Stefan

PS: It seems that also the width-statement (width:75) is not working with
the Internet Explorer.
================================================== ===========

<html>
<script type="text/javascript">
function InsertRow(WhereToInsert) {
var xtable
var xrow
var xcell
var xelement

xtable = document.getElementById("MyTable")
xrow = xtable.insertRow(WhereToInsert)

xcell = xrow.insertCell(0)
xcell.innerHTML = "Inserted Row"
xcell.setAttribute("bgColor", "#008888")

xcell = xrow.insertCell(1)
xcell.innerHTML = ""
xcell.setAttribute("bgColor", "#008888")
xelement = document.createElement("input")
xelement.setAttribute("type", "button")
xelement.setAttribute("style", "width:75")
xelement.setAttribute("value", "Click")
xelement.setAttribute("onClick", "alert('Wow, it works!')")
xcell.appendChild(xelement)
}
</script>

<body>
<form name="MyForm">
<table id = "MyTable" width = "400" align = "center">
<tr>
<td width = "50%" style = "background-color:#00ffff">
Text Row 1
</td>

<td width = "50%" style = "background-color:#00ffff">
<input type = "button" style = "width:75" value = "Click"
onClick = "alert('Wow, it works!')">
</td>
</tr>

<tr>
<td width = "50%" style = "background-color:#00ffff">
Text Row 2
</td>

<td width = "50%" style = "background-color:#00ffff">
<input type = "button" style = "width:75" value = "Click"
onClick = "alert('Wow, it works!')">
</td>
</tr>
</table>
<p>

Where to insert the new row:
<input type = "text" name = "InsertRowNumber" value = "">
<input type = "button" value = "Add Row" onClick =
"InsertRow(document.MyForm.InsertRowNumber.value)" >
</form>
</body>
</html>

================================================== ===========
Oct 30 '05 #1
6 4005


Stefan Mueller wrote:

xelement = document.createElement("input")
xelement.setAttribute("type", "button")
xelement.setAttribute("style", "width:75")
xelement.setAttribute("value", "Click")
xelement.setAttribute("onClick", "alert('Wow, it works!')")


setAttribute with script in HTML documents gives you all kind of cross
browser incompatibilities, it is usually easier and safer to script
element object properties e.g.
xelement.type = 'button';
xelement.style.width = '75px';
xelement.defaultValue = xelement.value = 'Click';
xelement.onclick = function (evt) {
alert('Wow, it works cross browser!');
};

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 30 '05 #2
Hello

You are great!!! I've been testing the whole day and now it works with both
browsers. Many thanks.

Do you perhaps know how to translate the following setAttribute (not working
with the Internet Explorer) to the format xelement.xxx?

xelement.setAttribute("class", "style_button")
xelement.setAttribute("onMouseover", "className =
'style_button_mouseover'")
xelement.setAttribute("onMouseout", "className = 'style_button'")
Do you also know how to add class, onMouseover and onMouseout to the whole
row:
In HTML I do:
<tr id="MyRow" class = "stil_tabelleneintrag"
onMouseover = "className = 'stil_tabelleneintrag_mouseover'"
onMouseout = "className = 'stil_tabelleneintrag'">

In JavaScript I guess I have to use xrow.xxx
xtable = document.getElementById("MyTable")
xrow = xtable.insertRow(WhereToInsert)

Stefan
Oct 30 '05 #3
Hello

You are great!!! I've been testing the whole day and now it works with both
browsers. Many thanks.

Do you perhaps know how to translate the following setAttribute (not working
with the Internet Explorer) to the format xelement.xxx?

xelement.setAttribute("class", "style_button")
xelement.setAttribute("onMouseover", "className =
'style_button_mouseover'")
xelement.setAttribute("onMouseout", "className = 'style_button'")
xelement.setAttribute("tabindex", "999")

Do you also know how to add class, onMouseover and onMouseout to the whole
row:
In HTML I do:
<tr id="MyRow" class = "stil_tabelleneintrag"
onMouseover = "className = 'stil_tabelleneintrag_mouseover'"
onMouseout = "className = 'stil_tabelleneintrag'">

In JavaScript I guess I have to use xrow.xxx
xtable = document.getElementById("MyTable")
xrow = xtable.insertRow(WhereToInsert)

Stefan
Oct 30 '05 #4


Stefan Mueller wrote:

xelement.setAttribute("class", "style_button")
The class attribute is scripted as the property className e.g.
xlement.className = 'style_button';
That is one of the few exceptions and was done because 'class' is a
keyword or reserved word in many programming languages.
xelement.setAttribute("onMouseover", "className =
'style_button_mouseover'")


Within HTML documents you can usually script HTML event handlers as
elementObject.oneventname = expressionYieldingAFunctionObject;
e.g.
xelement.onmouseover = function (evt) {
this.className = 'style_button_mouseover';
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 30 '05 #5
Great! It work's perfekt. Thanks again.

I was quite close. I've tried 'classname' instead of 'className'. It's very
confusing. Sometimes you have to use big letters (like in 'className') and
sometimes only small letters (like in 'onmouseover'). However, I know now
that I always have to try all versions.

What does 'evt' mean in '... = function(evt) {...}? Can I omit it?
I still have one problem with my inserted row. The text style is not
correct.
I HTML I do:
<td width = "50%" align = "center">
<h5 class = "style_table_entry">Text in cell</h5>
</td>

In JavaScript I do:
xcell = xrow.insertCell(2)
xcell.style.width = "50%"
xcell.align = "center"
xcell.className = "style_table_entry"
xcell.innerHTML = "New text in cell"

The text 'New text in cell' has not the style 'style_table_entry' because
this style is defined as 'h5.style_table_entry'. How can I add the new text
as '<h5>'?
I tried
xcell.innerHTML = "<h5> New text in cell </h5>"
but it does not work.

Many thanks
Stefan
Oct 30 '05 #6


Stefan Mueller wrote:

What does 'evt' mean in '... = function(evt) {...}? Can I omit it?
It is a formal parameter to that function used as an event handler where
certain implementations (Netscape, Mozilla, Opera) pass in the event
object when the event handler is called. If you don't need the event
object in your event handlers you can of course omit the formal
parameter. Having it is closer to what an event handler attribute in
markup translates to in code however.
How can I add the new text
as '<h5>'?


You can create any element object with document.createElement e.g.
var h5 = document.createElement('h5');
h5.appendChild(document.createTextNode('Kibology for all'));
xcell.appendChild(h5);

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 31 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lawrence | last post by:
Google can't find me a good example of how to use the "if exists" syntax in MySql. Is it right that to use it this way: INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk") I want...
1
by: Thomas Bartkus | last post by:
The meaning of REPLACE INTO is clear to me. IF the new record presents new key values, then it is inserted as a new record. IF the new record has key values that match a pre-existing record, then...
6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
3
by: Vic Sowers | last post by:
I'm trying to sort an HTML table (tBody) on one of its columns dynamically. My approach is to remove all the rows into an array, sort the array, and insert to rows back into the tBody. But,...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
5
by: Andrew | last post by:
Hi, friends, In ASP, we use obj = CreateObject("com.dll") obj.GetHTMLText(inVal, outHTMLTxt1, outHTMLTxt2) to get different HTML strings based on input values. Then, we insert them into...
28
by: Giggle Girl | last post by:
Can someone show me how to insert a row at any given row index of an already created table? It only has to work in IE6 (used on intranet at work). Specifically, if a table is 20 rows in total...
3
mmarif4u
by: mmarif4u | last post by:
Hi everybody, I already post a thread like this but that was in mysql. Here i have a problem with all the code,, <?php // signup.php include("common.php"); include("db.php");
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.