DOM-type query

  • Thread starter Dr J R Stockton
  • Start date
D

Dr J R Stockton

I have the following working code; BID is global, initially 0. POPBtn
is called during the loading of the page, maybe several times.

function POPBtn() { // Call with ArgList to generate button
var I = 'JJ' + BID++ // var BID = 0 precedes
document.write("<input type=button value='POP Code Up' ID=", I,
" onClick='POPThis(this)'> ")
document.getElementById(I).btnargs = arguments }

Is it possible, and if so how, to add the item btnargs with value
set to arguments to the new button without using the button's ID or
name? Note that onClick's argument is this . POPThis(X) uses
X.btnargs, of course.
 
M

Martin Honnen

Dr said:
function POPBtn() { // Call with ArgList to generate button
var I = 'JJ' + BID++ // var BID = 0 precedes
document.write("<input type=button value='POP Code Up' ID=", I,
" onClick='POPThis(this)'> ")
document.getElementById(I).btnargs = arguments }

Is it possible, and if so how, to add the item btnargs with value
set to arguments to the new button without using the button's ID or
name? Note that onClick's argument is this . POPThis(X) uses
X.btnargs, of course.

During page load, after each call to document.write, the input element
written should be last in document.getElementsByTagName('input') thus if
you don't want to use document.getElementById you could try
var inputs = document.getElementsByTagName('input');
inputs[inputs.length - 1].btnargs = arguments;

Whether it is a good idea to try to add properties to DOM objects is
another issue.
 
D

Dr J R Stockton

Dr said:
function POPBtn() { // Call with ArgList to generate button
  var I = 'JJ' + BID++ // var BID = 0 precedes
  document.write("<input type=button value='POP Code Up' ID=", I,
    " onClick='POPThis(this)'> ")
  document.getElementById(I).btnargs = arguments }
Is it possible, and if so how, to add the item   btnargs   with value
set to   arguments   to the new button without using the button's ID or
name?  Note that onClick's argument is   this .  POPThis(X)  uses
X.btnargs, of course.

During page load, after each call to document.write, the input element
written should be last in document.getElementsByTagName('input') thus if
you don't want to use document.getElementById you could try
   var inputs = document.getElementsByTagName('input');
   inputs[inputs.length - 1].btnargs = arguments;


Well, your code has the usual effect - it works as delivered.
Thanks. Uploaded.

It does appear to require the creation of an array for each call; I
was thinking that there ought to be a way of accessing the newly-
created button object more "locally" using some combination of Node,
parentNode, previousSibling, lastChild and suchlike.

Whether it is a good idea to try to add properties to DOM objects is
another issue.

Well, I want the visible object, the button on the screen, to possess
uniquely the property of knowing the argument list of the call of
POPBtn which created it. So giving the JavaScript object the
corresponding JavaScript property seems right. There is no need in
this case for concern about the meaning of the arguments possibly
changing between creation and use of the button.

A reasonably short page using PopBtn (the real name!) is <URL:http://
www.merlyn.demon.co.uk/zel-card.htm> - it does show a 68k PNG, but
that might amuse you as it is in German ... and not entirely correct
algorithmically.


Usenet propagation is not entirely well at present; your article has
not yet been delivered to Merlyn, which is why I am using Google for
this.
 
M

Martin Honnen

Dr said:
It does appear to require the creation of an array for each call; I
was thinking that there ought to be a way of accessing the newly-
created button object more "locally" using some combination of Node,
parentNode, previousSibling, lastChild and suchlike.

getElementsByTagName gives you a "live" DOM collection, not an array. As
a DOM collection is live you don't have to call getElementsByTagName
each time if you don't want to, you could put in a global
var inputs = document.getElementsByTagName('input');
before your function is called first and then in the function you could
simply use
inputs[inputs.length - 1].btnargs = arguments;
as "live" collection means the inputs variable is updated when the DOM
changes.

As for using lastChild or similar, the problem is that you don't have
any reference node. You would need e.g.
var scripts = document.getElementsByTagName('script');
var currentScript = scripts[script.length - 1];
var parentEl = currentScript.parentNode;
var input = parentEl.lastChild;
but that way you don't save anything really as you simply use
getElementsByTagName('script') instead of getElementsByTagName('input').

Of course with the W3C DOM you don't need to document.write an input to
create it, you could use e.g.
var input = document.createElement('input');
input.type = 'button';
// set further properties as needed
input.btnargs = arguments;
then you would however still need getElementsByTagName to find a
reference node to insert the input button:
var scripts = document.getElementsByTagName('script');
scripts[scripts.length - 1].parentNode.appendChild(input);
during page load at the right position.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
cor-online.net>, Sun, 4 Jan 2009 14:24:43, Martin Honnen
getElementsByTagName gives you a "live" DOM collection, not an array.
...
As for using lastChild or similar, the problem is that you don't have
...
Of course with the W3C DOM you don't need to document.write an input to
...

So your first suggestion can be approximately equalled, but not
significantly beaten. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,123
Messages
2,570,740
Members
47,295
Latest member
riya007

Latest Threads

Top