/    Sign up×
Community /Pin to ProfileBookmark

drawing inside div

I have this div called “frame” which acts as a container where I’m drawing some gifs

[CODE]
<center>
<div id=”frame”>
<form name = “inserimento” action=”” method=”POST”>
numero:<input type = “text” name = “input” value = “” size=”2″>
<input type = “submit” name =”pulsante” id = “nodo” value = “inserisci” onclick = “insert();”>
</form>
<script language = “javascript”>
//draw(); decomment to draw

</script>
</div>
</center>

</body>[/CODE]

The drawing is done by the function draw.

[CODE]
function draw(){
var newdiv = document.createElement(‘div’);
var divIdName = ‘prova’ + div_id;
div_id += 1;

newdiv.setAttribute(‘id’,divIdName);
newdiv.style.position = ‘absolute’;
newdiv.innerHTML = ‘<img src=”1.gif”>’;
newdiv.style.left = 0;
newdiv.style.top = 0;
document.getElementById(‘frame’).appendChild(newdiv);

}

[/CODE]

If I decomment the function draw everything is fine, with the gif being displayed. I want to do the same thing upon clicking the button pulsante by means of the event handler [I]insert[/I]

[CODE]
<form name = “inserimento” action=”” method=”POST”>
numero:<input type = “text” name = “input” value = “” size=”2″>
<input type = “submit” name =”pulsante” id = “nodo” value = “inserisci” onclick = “insert();”>
</form>

[/CODE]

This handler is as follows

[CODE]
function insert(){
draw();
}

[/CODE]

The problem is that nothing is drawn when calling the function insert. Why ??? ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 01.2009 — &lt;button type="button" name ="pulsante" id="pulsante" value="inserisci" onclick="insert();"&gt;inserisci&lt;/button&gt;
Copy linkTweet thisAlerts:
@cleanzeroauthorMar 01.2009 — &lt;button type="button" name ="pulsante" id="pulsante" value="inserisci" onclick="insert();"&gt;inserisci&lt;/button&gt;
[/QUOTE]


:eek: Why ?
Copy linkTweet thisAlerts:
@FangMar 01.2009 — Why are you submitting the form?
Copy linkTweet thisAlerts:
@cleanzeroauthorMar 01.2009 — Why are you submitting the form?[/QUOTE]

Yes I know, the form was useless, but shouldn't the function insert have worked ? However, it doesn't matter, now it works. Thanks.
Copy linkTweet thisAlerts:
@FangMar 01.2009 — &lt;input type = "submit" name ="pulsante" id = "nodo" value = "inserisci" onclick = "insert();"&gt;
This fires the onclick, displaying the image, then submits the form which reloads the document.

Use the button element. No need for the form element either.
×

Success!

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

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

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