473,804 Members | 4,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object model, referring to rows

I am unclear how to test this object I'm sending to my function. It is
a cell in a table and I simply want to know which row it is in. I am
not a javascript programmer but a javascript cobbler, so illuminating
me about the proper terminology would be greatly appreciated as well...

The answer would allow me to simplify this (working) snippet. Thanks!

_______________ _______________ _______________ _______________ _____________

<script language="JavaS cript" type="text/JavaScript">
function popForm(which){
d=document.getE lementById('loo kup');
u=document.getE lementById('upd ate');
for(var i=0;i<d.rows.le ngth;i++){
for(var j=0;j<d.rows[i].cells.length;j ++){
if(d.rows[i].cells[j]==which){
d.rows[i].style.backgrou ndColor='#FFCC0 0';
for(var jj=0;jj<d.rows[i].cells.length;j j++){
u['c'+jj].value = d.rows[i].cells[jj].firstChild.nod eValue;
}
}
}
}
}
</script>


</head>

<body>
<table width="75%" border="1" id="lookup">
<tr><td colspan="3">Loo kup Table</td></tr>
<tr>
<td onClick="popFor m(this);">100</td>
<td>300</td>
<td>500</td>
</tr>
<tr><td colspan="3">&nb sp;</td></tr>
</table>

<form method="post" action="" id="update">
<input type="text" name="a" id="c0">
<input type="text" name="b" id="c1">
<input type="text" name="c" id="c2">
</form>
</body>
</html>

Aug 23 '05 #1
6 2888
jh******@gmail. com wrote:
I am unclear how to test this object I'm sending to my function. It is
a cell in a table and I simply want to know which row it is in. I am
not a javascript programmer but a javascript cobbler, so illuminating
me about the proper terminology would be greatly appreciated as well...

The answer would allow me to simplify this (working) snippet. Thanks!

_______________ _______________ _______________ _______________ _____________

<script language="JavaS cript" type="text/JavaScript">
The language attribute is depreciated, just use type.
function popForm(which){
d=document.getE lementById('loo kup');
u=document.getE lementById('upd ate');
for(var i=0;i<d.rows.le ngth;i++){
for(var j=0;j<d.rows[i].cells.length;j ++){
if(d.rows[i].cells[j]==which){
d.rows[i].style.backgrou ndColor='#FFCC0 0';
for(var jj=0;jj<d.rows[i].cells.length;j j++){
u['c'+jj].value = d.rows[i].cells[jj].firstChild.nod eValue;
}
}
}
}
}
If 'works' means that the parent row of the cell has its background
colour changed, then:

function popForm( el ){
if ( ! el.style ) return;
while ( el.parentNode && 'tr' != el.nodeName.toL owerCase() ) {
el = el.parentNode;
}
el.style.backgr oundColor = '#FFCC00';
}

is the simple method. But even simpler is to put an onclick event on the
TR itself - it depends on what you are really trying to do.

The second part of your function appears to be trying to change the
values of the form controls - is that it? If so, you need to use the
form's elements collection.
</script>


</head>

<body>
<table width="75%" border="1" id="lookup">
<tr><td colspan="3">Loo kup Table</td></tr>
<tr>
<td onClick="popFor m(this);">100</td>
<td>300</td>
<td>500</td>
</tr>
<tr><td colspan="3">&nb sp;</td></tr>
</table>

<form method="post" action="" id="update">
<input type="text" name="a" id="c0">
<input type="text" name="b" id="c1">
<input type="text" name="c" id="c2">
</form>
</body>
</html>

--
Rob
Aug 23 '05 #2
ASM
jh******@gmail. com wrote:
I am unclear how to test this object I'm sending to my function. It is
a cell in a table and I simply want to know which row it is in. I am
not a javascript programmer but a javascript cobbler, so illuminating
me about the proper terminology would be greatly appreciated as well...
<table>
<tr>
<td>1</td>
<td
onclick="var r = this.rows;
for(var i=0;i<r.length; i++) for(var j=0;j<r[i].cells.length;j ++)
if(r[i].cells[j]==this)
{
alert('Row = 'i+1);
return;
}
">2</td>
<td>3</td>
</tr><tr><td>4</td><td>5</td><td>6</td></tr>
</table>
The answer would allow me to simplify this (working) snippet. Thanks!

_______________ _______________ _______________ _______________ _____________

<script language="JavaS cript" type="text/JavaScript">
function popForm(which){
function to do something with what object 'which'
d=document.getE lementById('loo kup');
d = object in page with an 'id' nammed 'lookup'
here d = the table
d (the table) contains n rows,
JS count them from 0 (zero insteed of one)
u=document.getE lementById('upd ate');
same with the form 'update' which is now 'u' (in JS memory)
for(var i=0;i<d.rows.le ngth;i++){
for i
growing from 0 to number of rows in table
(table = 'd' in JS memory)
(d.rows.length = number of rows in normal math)
for(var j=0;j<d.rows[i].cells.length;j ++){
then (with each case of i)
for j
growing from 0 to number of cells in row 'i'
(now JS instigates the line 1 if i=0)
if(d.rows[i].cells[j]==which){
if in this 'i'th row, the 'j'th cell is 'which'
('which' figures what object was clicked)
d.rows[i].style.backgrou ndColor='#FFCC0 0';
the 'i'th row's is colored in fc0
(its backround color style is ...)
for(var jj=0;jj<d.rows[i].cells.length;j j++){
always in this 'i'th row,
for jj
growing from 0 to number of cells in row 'i'
u['c'+jj].value = d.rows[i].cells[jj].firstChild.nod eValue;
the value of u element named : 'c' + jj
(that's to say form inputs nammed 'c0', then 'c1' then ... values)
becomes : value of first child of 'jj'th cell of 'i'th row

in this example :
- 'which' will be 1st cell in 1st row (where the click appends)
- only 1st row will be investigate
- in this row,
each cell input (firstChild.nod eValue) will be copied in a textbox
Each cell input copied in textbox of same row
(that's to say c0 = R1C1 input, c1 = R1C2 input, c2 = R1C3 input)
}
}
}
}
}
</script>


</head>

<body>
<table width="75%" border="1" id="lookup">
<tr><td colspan="3">Loo kup Table</td></tr>
<tr>
<td onClick="popFor m(this);">100</td>
<td>300</td>
<td>500</td>
</tr>
<tr><td colspan="3">&nb sp;</td></tr>
</table>

<form method="post" action="" id="update">
<input type="text" name="a" id="c0">
<input type="text" name="b" id="c1">
<input type="text" name="c" id="c2">
</form>
</body>
</html>

--
Stephane Moriaux et son [moins] vieux Mac
Aug 24 '05 #3
Thanks all very much for the information. I put the ugly thing together
based on a few examples, it is doing what I need it to do [highlight
the source row and populate a form with the data from that row] but I
knew it could be done much more elegantly if I really knew what I was
doing.

Thanks again!

Aug 24 '05 #4
Great guys! Now here's a useful finished test case; automatically
populating a form with data from a row; highlighting the source row
and reverting it back to it's default color if a new row is selected.
[chubby fingered managers..]

I'm ready to integrate the javascript into my php app! Thanks again.
<script type="text/JavaScript">
var oldrw;

function popForm(rw){

// preserve old cellcolor
if(oldrw) oldrw.style.bac kgroundColor=ol dbgc;
oldrw=rw; oldbgc=rw.style .backgroundColo r;

// highlight row
rw.style.backgr oundColor='#FFC C00';

u=document.getE lementById('upd ate');
for(var j=0;j<rw.cells. length;j++){
u['c'+ j].value = rw.cells[j].firstChild.nod eValue;
}
}
</script>
</head>

<body>
<table width="75%" border="1" id="lookup">
<tr><td colspan="3">Loo kup Table</td></tr>
<tr onClick="popFor m(this);">
<td>100</td>
<td>300</td>
<td>500</td>
</tr>
<tr onClick="popFor m(this);">
<td>cdf</td>
<td>xdd</td>
<td>12.3</td>
</tr>
<tr><td colspan="3">&nb sp;</td></tr>
</table>
<p>Update Table
<form method="post" action="" id="update">
<input type="text" name="a" id="c0">
<input type="text" name="b" id="c1">
<input type="text" name="c" id="c2">
</form>
</p>
</body>
</html>

Aug 24 '05 #5
Added a feature to this little jscript, now it not only highlights the
row and populates a form with that row's data, but if I give radio
buttons at the beginning of the row a name 'rw' combined with the row
number; I can check them off too. I'm certain there is a better way to
use this using the DOM to figure out which radio button is in my
triggering row, But I have to move on to other things now. [good enough
for government work as they say..]

I've also added a couple bugfixes in case cells in the source table are
empty or there is no corrolary target input for the data to go into in
the form..

<script type="text/JavaScript">
var oldrw;

function popForm(rw,val) {

// preserve old cellcolor
if(oldrw) oldrw.style.bac kgroundColor=ol dbgc;
oldrw=rw; oldbgc=rw.style .backgroundColo r;

// highlight row
rw.style.backgr oundColor='#FFC C00';

// check box
cb=document.get ElementById('rw '+val);
cb.checked='tru e';

u=document.getE lementById('upd ate');

for(var j=0;j<rw.cells. length;j++){
if(! u['c'+ j]) continue; if (! rw.cells[j].firstChild) continue;
u['c'+ j].value = rw.cells[j].firstChild.nod eValue;
}
}

</script>

Aug 24 '05 #6
jh******@gmail. com wrote:
Added a feature to this little jscript, now it not only highlights the
You probably want to carefully consider use of the term 'jscript' when
you likely mean 'javascript'.

Using a script element type attribute of 'text/jscript' will cause the
script not to be executed in many UAs (though not all - IE is happy with
it), whereas 'text/javascript' is accepted almost everywhere by UAs that
support scripting.

JScript is a registered trademark of Microsoft Corp. and is their
implementation of ECMAScript.

JavaScript was originally a registered trademark of Netscape
Communications but now belongs to Sun Microsystems, Inc. It is
implemented by almost everyone who makes a browser.
row and populates a form with that row's data, but if I give radio
buttons at the beginning of the row a name 'rw' combined with the row
number; I can check them off too. I'm certain there is a better way to
use this using the DOM to figure out which radio button is in my
triggering row, But I have to move on to other things now. [good enough
for government work as they say..]

I've also added a couple bugfixes in case cells in the source table are
empty or there is no corrolary target input for the data to go into in
the form..

<script type="text/JavaScript">
var oldrw;

function popForm(rw,val) {

// preserve old cellcolor
if(oldrw) oldrw.style.bac kgroundColor=ol dbgc;
oldrw=rw; oldbgc=rw.style .backgroundColo r;
oldbgc is being declared as a global variable, see below.

// highlight row
rw.style.backgr oundColor='#FFC C00';

// check box
cb=document.get ElementById('rw '+val);
cb is declared here as a global, is that necessary? It's good practice
to keep variables local unless they really do need to be global:

var cb = document.getEle mentById( 'rw' + val );
cb.checked='tru e';
The checked attribute is a boolean, so it should be set as one (though
using a string seems to work OK in some browsers):

cb.checked = true;
u=document.getE lementById('upd ate');
Global scope again:

var u=document.getE lementById('upd ate');
for(var j=0;j<rw.cells. length;j++){
if(! u['c'+ j]) continue; if (! rw.cells[j].firstChild) continue;
u['c'+ j].value = rw.cells[j].firstChild.nod eValue;
The use of continue can nearly always be avoided, try:

if( u['c'+j] && rw.cells[j].firstChild ) {
u['c'+j].value = rw.cells[j].firstChild.nod eValue;
}

If you were going to do lots and lots of these (say a few hundred) then:

var x, y;
...
for(var j=0, len=rw.cells.le ngth; j<len; j++){
if( ( x=u['c'+j] ) && ( y=rw.cells[j].firstChild ) ) {
x.value = y.nodeValue;
}
}

should save a bit of time by reducing the number of lookups, but for 10
or 20 you wont notice the difference.
}
}

</script>


--
Rob
Aug 25 '05 #7

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

Similar topics

1
1541
by: scottf35 | last post by:
Hello, Can someone please point me to THE source for complete javascript documentation? Particualry I am interested in the document object. For example with, document.getElementById(id), there is at least style and rows properties and with rows you can get lenght: eg document.getElementById(id).Style
5
2416
by: Ara.T.Howard | last post by:
anyone out there know where i might find a python object model diagram? cheers. -a -- =============================================================================== | email :: ara t howard noaa gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death
26
5694
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized several parts of the DOM, but this does not include the window object. Thank you
6
3677
by: Larry | last post by:
Hi All, I am in .NET 2003 I have a form and a toolbar on the form. When the user clicks on a button on the toolbar I call the following Sub I get a crash on the last line: tlbSearch.Buttons(0).Enabled = True ' Enable search
35
3237
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an instance of an object" for the line 'If mpg.FindControl("lkred").Visible = True Then'. I couldn't find sofar the solution. Any help would be appreciated ... Thanks
7
3585
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id - FirstName - LastName - CompanyId (many-to-one )
0
1163
by: =?Utf-8?B?SmVhbi1GcmFuY29pcyBCcmV0b24=?= | last post by:
"siddharthkhare@hotmail.com" wrote: The context is important in this kind of design concern : I assume there's a lot of user and that application will evolve to add richer functionality. My first concern would be to separate the business logic code from data contener : the new code would look like that : LineItemBusinessComponent.Delete(LineItemBusinessEntity) Exposing CRUD component isn't the best thing to do because your presentation...
275
12431
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
23
3153
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6856
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3823
muto222
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.