473,657 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

change element type with JS

hi all!

is it possible to change an element type with javacript for example
onclick to change a text box to a hidden element or to a textarea?

kind regards

marc

Aug 8 '06 #1
6 12758
libsfan01 said the following on 8/8/2006 4:40 AM:
hi all!

is it possible to change an element type with javacript for example
onclick to change a text box to a hidden element or to a textarea?
Did you try it?

Remove the element from the DOM and then re-add it.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 8 '06 #2
Hey Randy!

I have absolutely no idea how to remove and add to the DOM until now my
paradigm was that you couldnt do such a thing client-side. Pray tell
Randy how can i add an element to the dom within a named form?

Thanks again

Marc
Randy Webb wrote:
libsfan01 said the following on 8/8/2006 4:40 AM:
hi all!

is it possible to change an element type with javacript for example
onclick to change a text box to a hidden element or to a textarea?

Did you try it?

Remove the element from the DOM and then re-add it.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 8 '06 #3
is it possible to change an element type with javacript for example
onclick to change a text box to a hidden element or to a textarea?
the "type" property of an element is read-only (only have a getter),
thus cannot be modified. But, as Randy wrote, creating a new element,
setting it's value to the old one, then removing that old element is
how you should do it.

Consider the following :

------------------------------ start
--------------------------------------------
<script type="text/javascript">
function changeTextBox2T extArea( elementId ) {
var _el = document.getEle mentById( elementId );
var _parent = _el.parentNode;

// we remove the element from the DOM (optional as seen below...)
_parent.removeC hild( _el );

// property is read-only so we replace the element
if ( _el.type == 'text' ) {

// IE doesn't like too much playing with DOM, so we use the
innerHTML method.
// Using innerHTML replace all current elements inside _parent with
the new
// elements specified in the string.

_parent.innerHT ML = '<textarea id="' + _el.id + '">' + _el.value +
'</textarea>';
} else {
_parent.innerHT ML = '<input type="text" id="' + _el.id + '"
value="' + _el.value + '" />';
}

_el = null; // remove any reference to the old element
}
</script>

<input type="button" value="Change Text Box"
onclick="change TextBox2TextAre a('test');" />

<div>
<input type="text" id="test" />
</div>
---------------------------- end
-------------------------------------------------------

Here's some links that might interest you in learning the DOM
structure:

http://www.howtocreate.co.uk/tutoria...ript/dombasics
http://www.w3schools.com/htmldom/default.asp

.... look on Google, your best friend ;)

Hope this helps.

Aug 8 '06 #4
Yanick said the following on 8/8/2006 12:52 PM:
>is it possible to change an element type with javacript for example
onclick to change a text box to a hidden element or to a textarea?

the "type" property of an element is read-only (only have a getter),
thus cannot be modified. But, as Randy wrote, creating a new element,
setting it's value to the old one, then removing that old element is
how you should do it.
But that is not what your code does.
Consider the following :
<snip>
if ( _el.type == 'text' ) {

// IE doesn't like too much playing with DOM, so we use the
innerHTML method.
IE can "play with the DOM" quite well, you just have to learn how to do
it is all.

Here's some links that might interest you in learning the DOM
structure:

http://www.howtocreate.co.uk/tutoria...ript/dombasics
http://www.w3schools.com/htmldom/default.asp
I have yet to see anything in w3schools that I would consider worth
learning from when there are better resources.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 8 '06 #5

Randy Webb wrote:
the "type" property of an element is read-only (only have a getter),
thus cannot be modified. But, as Randy wrote, creating a new element,
setting it's value to the old one, then removing that old element is
how you should do it.

But that is not what your code does.
sure, I just replace it by the correct one. If you have an <input>
element that you want to change into a, for example, <divelement,
changing the type may cause trouble in a long run (if it's even
possible to do so anyways).

IE can "play with the DOM" quite well, you just have to learn how to do
it is all.
Oh, but I can too, but I found out that, IE had problems when your DOM
element is heavily customized / nested, so I try to balance both
worlds. Besides, working with innerHTML is somewhat faster...
I have yet to see anything in w3schools that I would consider worth
learning from when there are better resources.
Was I replying to you ? I thought that I was replying to the original
poster ? No offense to you, but I don't really care if you have some
"better ressources" (that you won't even care to point out, in fact).
Really, I have no grudge against you, nor do I have to proove anything
to you ; I'm simply trying to level out the help I'm giving to what I
think would be understandable by the original poster. You may agree
with me or not, but trying to proove that you're better than me (maybe
you are better than me, I really don't care) won't solve anything. Be
constructive, please.

Aug 12 '06 #6
Yanick said the following on 8/12/2006 11:47 AM:
Randy Webb wrote:
>>the "type" property of an element is read-only (only have a getter),
thus cannot be modified. But, as Randy wrote, creating a new element,
setting it's value to the old one, then removing that old element is
how you should do it.
But that is not what your code does.

sure, I just replace it by the correct one.
Then you agree that you aren't changing an element's type but that you
are removing it and replacing it?
If you have an <inputelement that you want to change into a, for
example, <divelement, changing the type may cause trouble in a
long run (if it's even possible to do so anyways).
Precisely. So you can't change an elements type, you remove it and
replace it. Or, swap them out using CSS.
>IE can "play with the DOM" quite well, you just have to learn how to do
it is all.

Oh, but I can too, but I found out that, IE had problems when your DOM
element is heavily customized / nested, so I try to balance both
worlds. Besides, working with innerHTML is somewhat faster...
innerHTML is somewhat faster sometimes, but not always. But that still
doens't mean that IE can't "play with the DOM" as it can. You just have
to use IE's DOM model and code is all.
>I have yet to see anything in w3schools that I would consider worth
learning from when there are better resources.

Was I replying to you ?
By your reasoning, you had no reason to reply to the OP as the question
wasn't directed at you. It's a flawed reasoning. This is Usenet, you
post, it gets replied to. Get used to it.
I thought that I was replying to the original poster?
And I replied to you, get used to it, it's Usenet. You post, it gets
discussed.
No offense to you, but I don't really care if you have some
"better ressources" (that you won't even care to point out, in fact).
There are two that are in my signature that get posted in every post I
make. Those two resources lead to numerous *quality* resources. So yes,
I point them out every time I post.
Really, I have no grudge against you, nor do I have to proove anything
to you ; I'm simply trying to level out the help I'm giving to what I
think would be understandable by the original poster.
And there is nothing wrong with that. But when there are better
resources, or the answer given is a sub-optimal answer, you can bet your
beer money it will be pointed out in this group.
You may agree with me or not,
I don't, but that should be obvious.
but trying to proove that you're better than me (maybe
you are better than me, I really don't care) won't solve anything.
Who said anything about somebody being better than somebody else? But by
the same token, pointing a person to a sub-optimal marginal resource
does more harm - in the long run - than it does good.
Be constructive, please.
Being constructive is a matter of a point of view. My post was very
constructive, if you fail to see that, then re-read it with an open mind
and try to learn from it. That is how I learned about half of what I
know about scripting is from people pointing out my mistakes and then me
learning from it.

There is not one person in this group that has answered more than 25
posts or so that has not been corrected in the past. Try finding someone
who doesn't fit that bill. It's part of this group and it is precisely
what makes this group as good as it is.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 12 '06 #7

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

Similar topics

1
1846
by: Alex Sab | last post by:
Hi, I am having trouble describing in a schema that elements can be mandatory in one part and not mandatory in another part of the xml document. Here is a sample xml file <Model:Templates> <Node:Template Node:nodeId="nodeID"> <Node:Name Node:Comment="">Name</Node:Name> <Node:Name2 Node:Comment="">Name</Node:Name2> ...
7
4840
by: Keith Smith | last post by:
How can I change the background IMAGE (not just color) of a CELL in a table? I know that I can do this using CSS, but I really need to be able to do it using JavaScript. Anyone know how? Must be able to change the image for each cell - not the whole table. Please help.
2
2967
by: Isz | last post by:
Hi Group: I would like to know if it is possible to change the name of an attribute to something else. My setup is like this: I have serveral SQL tables that I nest and join so that it all outputs into a nice XML file for the purpose of populating a flash navigation menu. <NewDataSet> <mainMenu_category menu_name="Home" menu_ID="1"> <menu menu_ID="1" />
2
2301
by: Daniel Lidström | last post by:
Hi, I would like to know the cleanest way to change the serialization of my Line class from: <Line staStart="2327.02" length="10.00000003390744"> <End>549016.570965 57945.741122</End> <Start>549019.590988 57955.274194</Start> </Line>
30
2076
by: Eric Lilja | last post by:
Hello, I have a function that has a static array with two elements in it. When the function is called the first time, it should access element 0, the second time it should access element 1, the third time element 0, the fourth element 1 etc. I can do it by having a static variable holding the index and and if/else-statement but is there a more clever way that doesn't need the if/else?
7
2086
by: David | last post by:
Hi, I'd really appreciate any help. Im trying to change a label over and over again. I can change it the first time, by using the following code, however it never works again, how can i do this so it uses the same element name? This is driving me insane. On the second call to var spanElm = document.getElementById("FirstNameLengthLabel"); spanElm is set to NULL. <script language=javascript>
1
8922
by: chrisdr | last post by:
I found this code in a previous post but I am not able to get this to work for me... I am trying to dynamically change an element's type from textbox to textarea with an event. Actually in my code i'm using a onClick event on a checkbox, but this is the original code exactly the way it was in the previous post... I tried to use it as it is and it errors out... Any input on how to make this code work or any other suggestions on how to accomplish...
4
6478
by: Dan | last post by:
H, i want to use for one specific application the default membership provider as defined by default in machine.config, except one element: i want to use requiresUniqueEmail="false" instead of the default configuration in machine.config (which is set on "true"). So i did this in the web.config of the application: <membership defaultProvider="MyMembershipProvider"> <providers>
5
4456
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know if I have the entirely wrong approach, but my code is below. Any or all help is appreciated! What currently happens is that the getdivision.php script works perfectly and gives me an entire table of correct data. The problem is that when I click...
0
8425
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
8326
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
8743
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
8522
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
7355
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
5647
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
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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
2
1973
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.