Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2012, 09:31 PM   PM User | #1
Drunklord
New Coder

 
Join Date: Sep 2011
Posts: 27
Thanks: 8
Thanked 0 Times in 0 Posts
Drunklord is an unknown quantity at this point
Question Drawing images in <div> fails

Hi all! I,m trying to insert images to <div>. I can insert the images in <body> of the document with this code
Code:
function drawImges(table_name,id,path){

  var s_table = document.getElementsByName(table_name);
 
  var newImage = new Image(),img;
  newImage.onload=function ()  {
    img=document.createElement('IMG');
    img.id=id;
    img.src=path;
    document.body.insertBefore(img,document.body.childNodes[0]);
     }
 newImage.src= path;

}
but I need them to be in <div>, not in <body> . I’ve tried it using
Code:
s_table.appendChild (img);
,
Code:
s_table.insertBefore(img,s_table.childNodes[0]);
instead of
Code:
document.body.insertBefore(img,document.body.childNodes[0]);
and all my intents fail.
Looking for help, please.
Drunklord is offline   Reply With Quote
Old 10-04-2012, 01:37 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
give the div an id and do
Code:
document.getElementById("mydiv").appendChild(img);
(getElementsByName will return a collection, even if it only of one element, so without seeing the rest of your code I guess you could do s_table[0].appendChild(img); but you would have to wonder why)

Last edited by xelawho; 10-04-2012 at 01:39 AM..
xelawho is offline   Reply With Quote
Old 10-04-2012, 09:38 AM   PM User | #3
Drunklord
New Coder

 
Join Date: Sep 2011
Posts: 27
Thanks: 8
Thanked 0 Times in 0 Posts
Drunklord is an unknown quantity at this point
Sorry, I`ve mistaken in coping my sript to the post , the original one do has div's name

"js/show_map.js" file
Code:
draw drawImges ("mapscreen", “img1”,”imges/001.jpg”,800,800);

function drawImges(table_name,id,path, width, height){

  var s_table = document.getElementsByName(table_name);
 
  var newImage = new Image(),img;
  newImage.onload=function ()  {
    img=document.createElement('IMG');
    img.id=id;
    img.src=path;
    document.body.insertBefore(img,document.body.childNodes[0]);

   document.getElementById(id).style.width = width + "px";
   document.getElementById(id).style.height = height + "px";
     }
 newImage.src= path;

}
HTML document
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Fails</title>
    
<script type="text/javascript" src=js/show_map.js></script>


</script>
</head>
<body>
<div  id="screen" name=”uuuu” style="width:600px;height:500px;">
</div>
<script>
  drawImges();
</script>

</body>

</html>
I’ve changed
Code:
document.body.insertBefore(img,document.body.childNodes[0]);
for
Code:
s_table[0].appendChild(img);
and it works. The image begins in <div id="screen" name=”uuuuh” style="width:600px;height:500px;">, but it’s spread all over the body and not inside of the div’s borders, as I pretend. I want to see a portion of the picture cut by dimensions of the div (not a scaled image).
What do I do wrong?

Last edited by Drunklord; 10-04-2012 at 09:41 AM..
Drunklord is offline   Reply With Quote
Old 10-05-2012, 04:59 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you need to set the containing div's overflow to hidden, otherwise it will stretch to accommodate the contents.

But the whole thing seems weird to me. I'd do it more like this:

Code:
<body>
<div id="screen" name="uuuu" style="width:600px;height:500px;overflow:hidden"></div>
<script type="text/javascript">
function drawImges(thediv,id,path, width, height){
  var newImage = new Image(),img;
  newImage.onload=function ()  {
    img=document.createElement('IMG');
    img.id=id;
    img.src=path;
	img.style.width = width + "px";
	img.style.height = height + "px";
   document.getElementById(thediv).appendChild(img)
     }
 newImage.src= path;
}
drawImges("screen", "img1","imges/001.jpg",800,800);
</script>
</body>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
Drunklord (10-05-2012)
Old 10-05-2012, 09:07 PM   PM User | #5
Drunklord
New Coder

 
Join Date: Sep 2011
Posts: 27
Thanks: 8
Thanked 0 Times in 0 Posts
Drunklord is an unknown quantity at this point
Thumbs up

It works Thank you very much
Drunklord is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:18 PM.


Advertisement
Log in to turn off these ads.