473,804 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Elements Created on the fly

I created an element on-the-fly using javascript like:

myA=document.cr eateElement("A" );
myA.href="Javas cript:acton(thi s)";
myA.className = "smallLink" ;
myText=document .createTextNode ('sometext');
myA.appendChild (myText);

Then when I pressed the link I'd like to change the 'className' of this
link from 'smallLink' to 'biggerLink', but nothing happens.

function action(obj)
{
//alert(obj);
obj.className=" biggerlink";
}

When I look at alert(obj) I get "[object]".
When I look at alert(obj.class Name) I get "undefined" .

Anyone know what I am doing wrong here?

Mike

Jul 20 '05 #1
17 1507
Maybe

return obj.className=" biggerlink";

--
George Hester
_______________ _______________ ____
"Michael Hill" <hi****@ram.lmt as.lmco.com> wrote in message news:40******** *******@ram.lmt as.lmco.com...
I created an element on-the-fly using javascript like:

myA=document.cr eateElement("A" );
myA.href="Javas cript:acton(thi s)";
myA.className = "smallLink" ;
myText=document .createTextNode ('sometext');
myA.appendChild (myText);

Then when I pressed the link I'd like to change the 'className' of this
link from 'smallLink' to 'biggerLink', but nothing happens.

function action(obj)
{
//alert(obj);
obj.className=" biggerlink";
}

When I look at alert(obj) I get "[object]".
When I look at alert(obj.class Name) I get "undefined" .

Anyone know what I am doing wrong here?

Mike

Jul 20 '05 #2
"Michael Hill" <hi****@ram.lmt as.lmco.com> wrote in message
news:40******** *******@ram.lmt as.lmco.com...
<snip>
myA=document.cr eateElement("A" );
myA.href="Javas cript:acton(thi s)";
myA.className = "smallLink" ;
myText=document .createTextNode ('sometext');
myA.appendChild (myText); <snip> function action(obj)
{
//alert(obj);
obj.className=" biggerlink";
}

<snip>

Javascript pseudo protocol HREFs are not methods of the created object
and are executed in the global scope, so - this - refers to the global
object and setting/creating a - className - property on the global
object is pointless.

You should never use javascript pseudo protocol HREFs:-

<URL: http://jibbering.com/faq/#FAQ4_24 >

- but to solve your specific problem you need to assign an onclick
method to the created object instead (though you will need the link to
have a HREF else it will not be a link, but then it probably could be a
SPAN element instead). E.g.:-

myA.onclick = function(){
this.className= "biggerlink ";
return false;
}

- or -

myA.onclick = new Funciton("this. className=\"big gerlink\";retur n
false;");

- depending on whether you understand the consequences of assigning an
inner function to an event handler or not (and/or whether the ECMA
Script compact profile will be relevant).

Richard.
Jul 20 '05 #3
>
- but to solve your specific problem you need to assign an onclick
method to the created object instead (though you will need the link to
have a HREF else it will not be a link, but then it probably could be a
SPAN element instead). E.g.:-

myA.onclick = function(){
this.className= "biggerlink ";
return false;
}


Richard,

Are you suggesting I change to something like:

mySPAN=document .createElement( "SPAN");
mySPAN.classNam e = "smallLink" ;
mySPAN.onclick = new Function("this. className=\"big gerlink\";retur n
false;");
myText=document .createTextNode ('sometext');
mySPAN.appendCh ild(myText);

Mike
Jul 20 '05 #4
"Michael Hill" <hi****@charter .net> wrote in message
news:10******** *****@corp.supe rnews.com...

- but to solve your specific problem you need to assign an
onclick method to the created object instead (though you will
need the link to have a HREF else it will not be a link, but
then it probably could be a SPAN element instead). E.g.:-

myA.onclick = function(){
this.className= "biggerlink ";
return false;
}
Are you suggesting I change to something like:

mySPAN=documen t.createElement ("SPAN");
mySPAN.classNa me = "smallLink" ;
mySPAN.oncli ck = new Function("this. className=\"big gerlink\";
return false;");
I assume you realise that the preceding line should not wrap.
myText=document .createTextNode ('sometext');
mySPAN.appendC hild(myText);


I don't have enough information about the context to make that
judgement. But the original code seemed to be attempting to create an
element that, when clicked on, change CSS styles but did nothing else
and given that browsers that support creating new elements also support
onclick events on most elements (including SPANs) a SPAN element might
be better suited to the task.

The important things are to never use a javascript pseudo-protocol HREF
if it is not going to directly result in the replacement of the current
page, and that if you want to refer to an element with the - this -
keyword you can only do so from within a function that is a method of
the element, not code that is executed in the global context as a side
effect of clicking on it. (and never even consider doing anything
proposed by George Hester. Doing the opposite, or just banging your head
on a desk a few times, would always be more productive.)

Beyond that the type of element used depends on the design and
specification for the task. But an A element has inherent behaviour that
users understand an indicating a link with which they can navigate to
another page and undermining the user's expectation is generally not
considered good UI design.

Richard.
Jul 20 '05 #5
Jesus Richard why continue bashing me? What's the purpose? I been bashed I been made to eat dog meat you won! Are you a happy camper now? What's wrong with you? You don't like me that's fine. You need to tesase like a juvinelle. Are you that insecure that all in the world must know you dislike like? Grow up man!@!!

--
George Hester
_______________ _______________ ____
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message news:bv******** ***********@new s.demon.co.uk.. .
"Michael Hill" <hi****@charter .net> wrote in message
news:10******** *****@corp.supe rnews.com...

- but to solve your specific problem you need to assign an
onclick method to the created object instead (though you will
need the link to have a HREF else it will not be a link, but
then it probably could be a SPAN element instead). E.g.:-

myA.onclick = function(){
this.className= "biggerlink ";
return false;
}

Are you suggesting I change to something like:

mySPAN=documen t.createElement ("SPAN");
mySPAN.classNa me = "smallLink" ;
mySPAN.oncli ck = new Function("this. className=\"big gerlink\";
return false;");


I assume you realise that the preceding line should not wrap.
myText=document .createTextNode ('sometext');
mySPAN.appendC hild(myText);


I don't have enough information about the context to make that
judgement. But the original code seemed to be attempting to create an
element that, when clicked on, change CSS styles but did nothing else
and given that browsers that support creating new elements also support
onclick events on most elements (including SPANs) a SPAN element might
be better suited to the task.

The important things are to never use a javascript pseudo-protocol HREF
if it is not going to directly result in the replacement of the current
page, and that if you want to refer to an element with the - this -
keyword you can only do so from within a function that is a method of
the element, not code that is executed in the global context as a side
effect of clicking on it. (and never even consider doing anything
proposed by George Hester. Doing the opposite, or just banging your head
on a desk a few times, would always be more productive.)

Beyond that the type of element used depends on the design and
specification for the task. But an A element has inherent behaviour that
users understand an indicating a link with which they can navigate to
another page and undermining the user's expectation is generally not
considered good UI design.

Richard.

Jul 20 '05 #6
On Thu, 29 Jan 2004 15:37:07 GMT, George Hester <he********@hot mail.com>
wrote:

[Fixed and trimmed top-post]
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:bv******** ***********@new s.demon.co.uk.. .
The important things are to never use a javascript pseudo-protocol
HREF if it is not going to directly result in the replacement of
the current page, and that if you want to refer to an element with
the - this - keyword you can only do so from within a function that
is a method of the element, not code that is executed in the global
context as a side effect of clicking on it. (and never even
consider doing anything proposed by George Hester. Doing the
opposite, or just banging your head on a desk a few times, would
always be more productive.)


Jesus Richard why continue bashing me? What's the purpose? I been
bashed I been made to eat dog meat you won! Are you a happy camper
now? What's wrong with you? You don't like me that's fine. You
need to tesase like a juvinelle. Are you that insecure that all in
the world must know you dislike like? Grow up man!@!!


Actually, I believe the point of Mr Cornford's comments were to warn the
OP against following your useless advice. Did you actually try your
suggestion? I doubt it, for if you did, you would find that the entire
page would be replaced by the text, biggerlink. I might normally attempt
to explain why but the last time I offered you the explanation to a
problem, you told me, though more tersely, to f*** myself.

Mr Cornford's remarks about the OP "banging his head" are quite correct;
it would have been more productive than your suggestion. At least the OP
would only have the current situation to continue with, not the added
worry of why his page suddenly disappeared.

Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #7
> Javascript pseudo protocol HREFs are not methods of the created object
and are executed in the global scope, so - this - refers to the global
object and setting/creating a - className - property on the global
object is pointless.

You should never use javascript pseudo protocol HREFs:-


Richard,

I was thinking about this some more and I thought I'd let you know that I
totally agree. I am using this though to open another window so it should
be ok to use a link in this case.

Here's that code modified some more:

I happen to be iterating over an array and producing some rows here,
putting in a link because I want the use to be able to click on them and
open a window:

<snip>
myA=document.cr eateElement("A" );
myA.href="Javas cript:void();"
myA.className = "smallLink" ;
//here I am creating bogus attributes and tieing them to the link object,
I'll need them later
myA.tmp1 = child_array[j][i-2]; // this is the parent number that is
key in the database
myA.tmp2 = child_array[j][i-1]; // this is the child number that is
also part of the key in the database
myA.onclick = function (evt) { this.className= "smallred";
edit_action(thi s.tmp1,this.tmp 2); return false; };
// this is the text title
myText=document .createTextNode (child_array[j][i]);
// Appends the text node to the td
myA.appendChild (myText);
<snip>

function edit_action(p_i d,id)
{
var win = window.open("so mepgm?p_num="+p _id+"&c_num="+i d,<snip>
win.focus();
}

1) do you think this is a proper use, now?
2) this code works, but doesn't change the color of the link until after
the window opens. I want the link to change color immediately so the user
knows "something" is happening. Sometimes it takes the users window a min
to open. I don't want the user to be clicking on this over and over.

Thanks for your comments.

Mike

Jul 20 '05 #8
"George Hester" <he********@hot mail.com> wrote in message
news:DM******** *******@twister .nyroc.rr.com.. .
Jesus Richard why continue bashing me? What's the purpose?

<snip>

As Mr Winter correctly surmised, my comments were intended as a warning
to the OP rather than a superfluous effort to bash you.

You have made it abundantly clear (by belittling and directly insulting
everyone who has ever offered you any good advice here) that you are
completely confident in the validity of your coding style. Based as it
is on scouring the Internet and newsgroups for any old half-ass hack
that superficially satisfies your criteria for a solution and then
jamming them into web pages without any real understanding of the code
used, its mechanism or the consequences. And that you don't see any need
to do anything differently.

Eventually its inevitable consequences will demonstrate to you why your
whole approach is so spectacularly wrong. And when that happens the only
people left willing to talk to you will not be capable of doing any more
than offering you more half-ass hacks, as you have already reached a
position where none of the people on this group who know how to properly
implement a browser script are willing to bother with you at all. And
you have stated that that suites you just fine.

But as your hack agglomerate approaches its inevitable fatal complexity
threshold you are inventing fairy tails to explain the artefacts of the
ensuing chaos, and as a result are in the unique position of knowing
less and less about browser scripting as time goes by.

There is nothing that can be done about that (and ever less people
willing to try). That doesn't matter, you can rattle about in your own
misconceptions to you harts content. But when you start bothering other
people with your nonsense they deserve to be warned that the best that
they can expect from you is the worst approach available, that normally
what you say is an baseless fantasy and at worst your suggestions will
be positively harmful.

Richard.
Jul 20 '05 #9
"Michael Hill" <hi****@ram.lmt as.lmco.com> wrote in message
news:40******** *******@ram.lmt as.lmco.com...
<snip>
myA=document.cr eateElement("A" );
myA.href="Javas cript:void();"
It is probably harmless here because the onclick handler returns false
and cancels the navigation and without JavaScript enabled the A element
will never be created but javascript pseudo-protocol HREFs really should
be avoided as a matter of habit.
myA.classNam e = "smallLink" ;
//here I am creating bogus attributes and tieing them to
//the link object, I'll need them later
myA.tmp1 = child_array[j][i-2]; // this is the parent
//number that is key in the database
myA.tmp2 = child_array[j][i-1]; // this is the child
//number that is also part of the key in the database myA.onclick = function (evt) { this.className= "smallred";
edit_action(th is.tmp1,this.tm p2); return false; };
This is OK so long as you are aware that assigning an inner function as
an event handler will produce a closure and that has implications
(particularly provoking the memory leak bug on IE browsers).

If you are creating numerous A elements with identical onclick functions
and having them retrieve the data they need from expando properties on
those elements then you could create a separate named function
definition and assign a reference to it to the onclick properties.

// this is the text title
myText=document .createTextNode (child_array[j][i]);
// Appends the text node to the td
myA.appendChil d(myText);
<snip>

function edit_action(p_i d,id)
{
var win = window.open("so mepgm?p_num="+
p_id+"&c_num="+ id,<snip>
Not all browsers support a window.open function and calling it
unverified on one of those browsers will generate an error.
win.focus();
}

1) do you think this is a proper use, now?
Yes, in principal if clicking on a link is going to result in the
display of a new page then that is probably within the expectations of
the users, even if that page is in a new window. Of course they may
still be confused if they happen to be operating a pop-up blocker or a
browser with that feature built in.

Generally the issues surrounding the consequences of the common use of
pop-up blocking mechanisms makes me always recommend never even
attempting to open a new window with a script. Even the most reliable
window opening script ever presented on this group is nowhere near
reliable enough for practical use.
2) this code works, but doesn't change the color of the link
until after the window opens. I want the link to change color
immediately so the user knows "something" is happening.
Sometimes it takes the users window a min to open. I don't
want the user to be clicking on this over and over.


You could experiment with using onmousedown to set the className for the
element and onclick to open the new window, or a CSS style for the
active link but it might just be that the browser/system is too busy
opening the new window to re-draw the existing one with the new styles.

It has been observed that changing the className property of an element
causes the browser to do considerably more work than would be required
by just making the relevant changes to the element's style object.

But I would not expect a long delay between opening a new window and its
contents starting to download to delay re-drawing of the original
window, there should be sufficient system resources available for the
existing window to be re-drawn while the new window is waiting for a
response to its initial request.

Richard.
Jul 20 '05 #10

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

Similar topics

13
371
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a base class for the worker class, and made up some functions/delegates to handle the invoke stuff for the UI and that was fine for a prototype. I rewrote this chunk, broke things out into different classes - but the threading is still the same - and...
1
3142
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
7
7004
by: Lau Lei Cheong | last post by:
Hello, I'm using javascript's insertAdjacentHtml() to insert images to the webform at runtime. This runs fine(image successfully displayed at the browser) but when I tried to access the control's src using FindControl() function in the code-behind, it seems that the control doesn't even exist. I know that I can add controls on the server-side, but then a postback will be needed which is the thing I want to avoid. Does anyone have idea...
6
2166
by: Vikram | last post by:
I have added some input elements on a page using javascript at client side. when i submit the page, i am unable to access the values of input elements created using request.form. Are elements created using javascript are available at the server? if not how can i do the above thing so that there values can be accessed. I dont want to postback page everytime i need to create a new element.
53
4763
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
3
6360
by: SMH | last post by:
Normally an SVG document is loaded/parsed/interpreted inside an HTML document using an 'object' (or 'embed') element, although there are supposedly other ways too. The problem is, the SVG document must be static this way. I want to use the DOM interface to build SVG dynamically inside an HTML document. I am guessing I can build it inside HTML within an 'object' (or maybe 'iframe'?) element. My intentions/goals:
2
2709
by: Roy Chastain | last post by:
I have some 'base' schema definitions that are going to be in several projects in the future. The processing of resulting classes created by XSD will be consistent across the new projects. I have created a schema with these base definitions and common code to process these elements. I have managed to finally get include to work so that the base schema is now included in the schema for a particular application. The base schema and...
6
3220
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, can someone please let me know how I can read xml elements using object oriented program. I created a class to use the get and set properties however I dont know how I can pass the values from xml file to the class and use the values in my form. Thanks -- Nejadian
1
4616
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in Database but I'm stuck in the EDIT. I'm getting 2 problems over here. Below is the description: 1)The FIRST page will list all the records from the table which Admin can EDIT with CHECKBOX for each record to select. He can select one or more than one...
0
9715
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
9595
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
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10097
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...
1
7642
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
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
5535
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...
1
4313
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 we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.