Get img from cell in Firefox

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

Hello all!

I'm trying to change the src of imgs in 5 different cells so I iterate
through the cells in the table and do this:
var img = table.rows[0].cells[n].firstChild;
img.src = some url

This works fine in IE but not in any other browser. What's the trick here?

Thanks,
Joe
 
Actually I guess it's rendered as an input. I'm using ImageButtons that I'm
trying to change.
 
Hi Joe,

As for loop through all the image(or other html elements in a html table),
I would suggeest you use the "document.getElementByTagName" function as
it's the standard DOM interface which works on both IE and firebox. The "
table.rows[0].cells[n].firstChild;" syntax is somewhat coupled with IE
browser. Here is a simple test page demonstrate this:

===========================================
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function get_img_in_table(tbid)
{
var tb = document.getElementById(tbid);

var imgs = document.getElementsByTagName("IMG");

var i =0;

for(i=0;i<imgs.length;++i)
{
alert(imgs.src);
imgs.alt = imgs.src;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbImages" >
<tr>
<td>image:</td>
<td><img
src="http://www.asp.net/App_Themes/Standard/i/logo.png" /></td>
</tr>

<tr>
<td>image:</td>
<td><img
src="http://static.asp.net/asp.net/images/books/book13.gif" /></td>
</tr>

<tr>
<td>image:</td>
<td><img src="http://ads.asp.net/ads/200_50_aspnet_gr.gif"
/></td>
</tr>
</table>

<br />
<input type="button" value="get image in table"
onclick="get_img_in_table('tbImages');" />
</div>
</form>
</body>
==============================================

BTW, for your new question about setting the "title", would you tell me
what' the title you mentioned? is it the page title or title of some other
html element on page?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Joe" <[email protected]>
References: <[email protected]>
Subject: Re: Get img from cell in Firefox
Date: Wed, 2 Apr 2008 17:30:30 -0400

Now it looks like my problem is I cannot get/set the title.

Joe said:
Hello all!

I'm trying to change the src of imgs in 5 different cells so I iterate
through the cells in the table and do this:
var img = table.rows[0].cells[n].firstChild;
img.src = some url

This works fine in IE but not in any other browser. What's the trick here?

Thanks,
Joe
 
Hello all!

I'm trying to change the src of imgs in 5 different cells so I iterate
through the cells in the table and do this:
var img = table.rows[0].cells[n].firstChild;
img.src = some url

This works fine in IE but not in any other browser. What's the trick here?

Thanks,
Joe

Hi Joe,

Little addition to the Steven's post.
document.getElementsByTagName("IMG") returns all images on a page.
table, tr, td elements also have getElementsByTagName method and you
can reduce result images set if you use it.

Regarding of firstChild.
In Firefox it depends on HTML structure, if there is a gap between td
and img tags then the first child is a text node.
1. <td><img ... - firstChild is image
2. <td> <img ... - firstChild is text node

Regards,
Mykola
http://marss.co.ua
 
The getElemenetByTagName worked to fix my issue. Thanks.

Steven Cheng said:
Hi Joe,

As for loop through all the image(or other html elements in a html table),
I would suggeest you use the "document.getElementByTagName" function as
it's the standard DOM interface which works on both IE and firebox. The "
table.rows[0].cells[n].firstChild;" syntax is somewhat coupled with IE
browser. Here is a simple test page demonstrate this:

===========================================
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function get_img_in_table(tbid)
{
var tb = document.getElementById(tbid);

var imgs = document.getElementsByTagName("IMG");

var i =0;

for(i=0;i<imgs.length;++i)
{
alert(imgs.src);
imgs.alt = imgs.src;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbImages" >
<tr>
<td>image:</td>
<td><img
src="http://www.asp.net/App_Themes/Standard/i/logo.png" /></td>
</tr>

<tr>
<td>image:</td>
<td><img
src="http://static.asp.net/asp.net/images/books/book13.gif" /></td>
</tr>

<tr>
<td>image:</td>
<td><img src="http://ads.asp.net/ads/200_50_aspnet_gr.gif"
/></td>
</tr>
</table>

<br />
<input type="button" value="get image in table"
onclick="get_img_in_table('tbImages');" />
</div>
</form>
</body>
==============================================

BTW, for your new question about setting the "title", would you tell me
what' the title you mentioned? is it the page title or title of some other
html element on page?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Joe" <[email protected]>
References: <[email protected]>
Subject: Re: Get img from cell in Firefox
Date: Wed, 2 Apr 2008 17:30:30 -0400

Now it looks like my problem is I cannot get/set the title.

Joe said:
Hello all!

I'm trying to change the src of imgs in 5 different cells so I iterate
through the cells in the table and do this:
var img = table.rows[0].cells[n].firstChild;
img.src = some url

This works fine in IE but not in any other browser. What's the trick here?

Thanks,
Joe
 
Thanks for your reply Joe,

I'm glad that it helps you.

Have a nice day!

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
========================================
Subject: Re: Get img from cell in Firefox
Date: Thu, 3 Apr 2008 12:06:39 -0400
The getElemenetByTagName worked to fix my issue. Thanks.

Steven Cheng said:
Hi Joe,

As for loop through all the image(or other html elements in a html table),
I would suggeest you use the "document.getElementByTagName" function as
it's the standard DOM interface which works on both IE and firebox. The "
table.rows[0].cells[n].firstChild;" syntax is somewhat coupled with IE
browser. Here is a simple test page demonstrate this:

===========================================
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function get_img_in_table(tbid)
{
var tb = document.getElementById(tbid);

var imgs = document.getElementsByTagName("IMG");

var i =0;

for(i=0;i<imgs.length;++i)
{
alert(imgs.src);
imgs.alt = imgs.src;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbImages" >
<tr>
<td>image:</td>
<td><img
src="http://www.asp.net/App_Themes/Standard/i/logo.png" /></td>
</tr>

<tr>
<td>image:</td>
<td><img
src="http://static.asp.net/asp.net/images/books/book13.gif" /></td>
</tr>

<tr>
<td>image:</td>
<td><img src="http://ads.asp.net/ads/200_50_aspnet_gr.gif"
/></td>
</tr>
</table>

<br />
<input type="button" value="get image in table"
onclick="get_img_in_table('tbImages');" />
</div>
</form>
</body>
==============================================

BTW, for your new question about setting the "title", would you tell me
what' the title you mentioned? is it the page title or title of some other
html element on page?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Joe" <[email protected]>
References: <[email protected]>
Subject: Re: Get img from cell in Firefox
Date: Wed, 2 Apr 2008 17:30:30 -0400

Now it looks like my problem is I cannot get/set the title.

Hello all!

I'm trying to change the src of imgs in 5 different cells so I iterate
through the cells in the table and do this:
var img = table.rows[0].cells[n].firstChild;
img.src = some url

This works fine in IE but not in any other browser. What's the trick here?

Thanks,
Joe

 
Back
Top