/    Sign up×
Community /Pin to ProfileBookmark

Questions on DOM objects

Hello. I’m trying to familiarize with using DOM. I’ve done a lot of javascript and I used to avoid it, aside from using basic document functions (document.getElementByID, document.getElementsByName). I do a lot of dynamic building on pages..i.e. my pages allow addition of more elements on user request, for which DOM seems well suited for. I never used it though but instead always used (some div).innerHTML +=”(new element code)”.

A major drawback I find in creation with DOM is the excess amount of code I would need to create everything. For example, suppose I had a div container IDed by “div1” for which I wanted to add a text input.
Without dom I’d write:

[CODE]
var inputCode = “<input type=”text” name=”textInput” value=”initVal”>”;
document.getFieldById(“div1).innerHTML+= inputCode;
[/CODE]

Just a couple of lines of code. But with DOM:

[CODE]
var inputElm = document.createElement(“input”)
inpuElm.setAttribute(“type”,”text”)
inpuElm.setAttribute(“name”,”textInput”)
inpuElm.setAttribute(“value”,”initVal”)
document.getFieldById(“div1).appendChild(“inputElm”)
[/CODE]

There is significantly more coding. With more elements that are more complicated, I’d imagine an obscene amount of javascript code using DOM. Is there a way to create an element and all its features in one go by adding all its coding at once, similar to my first approach?

Thanks!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Typhoon101May 03.2007 — That is the correct way to create an element using DOM, so no, there is not really a shorter way. You could of course enter that script into a function that is called for every element you wish to add.

For example
[code=html]
function doInput(type, name, value){
inputElm = document.createElement("input")
inpuElm.setAttribute("type",type)
inpuElm.setAttribute("name",name)
inpuElm.setAttribute("value",value)
document.getFieldById("div1).appendChild("inputElm")}[/code]


Then you could call the function by simply using this in your body somewhere.[code=html]<script type="text/javascript">doInput("text", "textInput", "initVal")</script>
<script type="text/javascript">doInput("password", "passwordInput", "initVal")</script>[/code]


You could even get clever and create an array of new fields then you would only need to call the function once.
Copy linkTweet thisAlerts:
@mrhooMay 03.2007 — where did you get document.getFieldById?
Copy linkTweet thisAlerts:
@mrhooMay 03.2007 — With a document.createElement function you can write something like this-

domNew('input','div1','textInput','','value=initVal')
Copy linkTweet thisAlerts:
@vinays84authorMay 03.2007 — Dammit..I knew I couldn't have my cake and eat it too. Yea, I guess I'll have to make use of a workaround, as you suggested.

What's your opinion on a combination of the two? Create temporary div, plop in the code, then steal the element for its actual use:
[CODE]
function createElement (elementCode) {
var tmpDiv = document.createElement("div")
tmpDiv.innerHTML+=elementCode;
return tmpDiv.firstChild;
}

var inputCode = "<input type="text" name="textInput" value="initVal">";
document.getFieldById("div1").appendChild(createElement(inputCode));

[/CODE]

It seems to work fine. Do you see and problems with this approach that I'm missing?
Copy linkTweet thisAlerts:
@vinays84authorMay 03.2007 — Sorry mrhoo..didn't see your replys. getFieldById is the same as getElementById..its just a function I had made myself.
×

Success!

Help @vinays84 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...