/    Sign up×
Community /Pin to ProfileBookmark

Manipulating where in HTML JS writes HTML

I’m wrestling trying to get my JS to write its HTML into a certain point in the HTML, namely just after the link that calls the JS, and before the final HTML component, at the angry face:

[code=html]
<img src=”dog.jpg” onclick=”catdog”(‘dog’)” />
<img src=”cat.jpg” onclick=”catdog”(‘cat’)” />
😡
</form>
<a href=”javascript:marksheet()”>How did it go?</a>
</body>
</html>

[/code]

So far we have a dog and a cat next to each other. I’m trying to add in a duck and a cow. Here’s the JS so far:

[CODE]
var cow = document.createElement(“img”);
var duck = document.createElement(“img”);
cow.setAttribute(“src”,”cow.jpg”);
duck.setAttribute(“src”,”duck.jpg”);
var docBody = document.getElementsByTagName(“body”)[0];
docBody.appendChild(cow);
docBody.appendChild (duck);
[/CODE]

It seems to always add on the duck and cow pics at the very end of the body – so I guess the line:

docBody.appendChild(cow);

needs amending, but I don’t know how exactly. All thoughts much obliged.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledJan 21.2009 — i guess it come handy to first understand the html dom. basically every element have a parent and may contain child. also, most of them share some methods and properties. however, the point is that using DOM methods on a given element may only affect the same level hierarchy of that given element. in your case you are appending the new element to the end of the body hierarchy, first because that is how appendChild work and second because you called the method within the body element.

so, what you have to do is append the elements to the same hierarchy level that the other images are contained. in your case i assume it is the form element. so, basically you would get a reference of the form element and then call the appendChild method within that reference:

<i>
</i>document.forms[0].appendChild(cow);
document.forms[0].appendChild(duch);

in this code i'm using forms[0] because browser commonly define an array that contain every form in the document.
Copy linkTweet thisAlerts:
@MrNobodyJan 21.2009 — One way is to put a DIV in your HTML where you want the content to go and, then, use the [B]appendChild[/B] method against that DIV object. The other way is to use the [B]insertBefore[/B] method to place the child at the appropriate point in the parent object.
Copy linkTweet thisAlerts:
@javascriptloverauthorJan 21.2009 — excellent, thanks for those tips.
×

Success!

Help @javascriptlover 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...