Click here to Skip to main content
12,510,988 members (59,595 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
Here is the code:
<script language="javascript" type="text/javascript">
    function ChangeImage(FirstID, SecondID) {
 
        document.write(FirstID + "_" + SecondID);
        var ClickedImage = document.getElementById(FirstID);
        var img = document.getElementById(SecondID);
        var changeURL = img.src;  <--       
        img.src = ClickedImage.src;

        ClickedImage.src = changeURL.src;

 
    }
</script>

The Error "Microsoft JScript runtime error: Object required" jumps
on the line where the arrow is.

How to fix that?
Posted 18-Jul-11 11:50am
KUFIKU337
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Apparently, this is because img == null at this point; and this is because you don't have any element with name SecontID at the moment of the call img = document.getElementById(SecondID). You could have miscalculated SecondID or never had attribute id with expected value in any of the elements. Where is your HTML?

How about using Javascipt debugger? It can be used with Visual Studio.

—SA
  Permalink  
Comments
Espen Harlinn 18-Jul-11 18:09pm
   
Good reply - my 5 - maybe the script runs before the page is loaded ...
SAKryukov 18-Jul-11 18:17pm
   
Thank you, Espen.
--SA
KUFIKU 18-Jul-11 18:43pm
   
Thank you for your reply.Here are 2 sample images I try to change

<td align="center"> <asp:ImageButton ID="ImageButton32" ImageUrl="BattleLine/images/troopback.jpg" runat="server" width="100px" Height="140px" onclick="ImageButton_Click"/> </td>

<td align="center"> <asp:ImageButton ID="ImageButton33" ImageUrl="BattleLine/images/troopback.jpg" runat="server" width="100px" Height="140px" onclick="ImageButton_Click"/> </td>

Here is the event:

protected void ImageButton_Click(object sender, ImageClickEventArgs e) {

ImageButton img = (ImageButton)sender; string imgID = img.ID;

if (ClickedImageID == "") ClickedImageID = imgID; else

{

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", "ChangeImage('"+ClickedImageID+"','"+imgID+"');",true);

ClickedImageID = ""; } }Edit:

Don't mind they have the same ImageUrl now, it is changed before the event runs, but still same error.
(I changed changeURL.src to changeURL now, but the problem is with the img as you said)
KUFIKU 18-Jul-11 19:44pm
   
I presented img.src in delay of 1.5 second and it writes "undefined"
Still does not work.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

insead of img.ID use img.ClientID
  Permalink  
Comments
KUFIKU 18-Jul-11 20:18pm
   
But it presented the IDS well
Edit:
I'll try and post if it helped, thanks.
KUFIKU 18-Jul-11 20:32pm
   
It writes the src of the images well but the images don't change.
Edit:
Microsoft JScript runtime error: Permission denied
DipaliKolhe 18-Jul-11 20:51pm
   
if its a ImageButton then set img.ImageUrl property
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

if its a ImageButton then set img.ImageUrl property
  Permalink  
Comments
KUFIKU 18-Jul-11 21:08pm
   
It didn't work
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

here is the code. this works perfectly fine for me. do not add any code in codebehind file.

<script type="text/javascript" language="javascript">
     function ChangeImage1(Image1) {
        var Image2 = $get('<%=btnImage2.ClientID%>')
        var tempURL = Image2.src;
        Image2.src = Image1.src;
        Image1.src = tempURL;
         return false;
     }
     function ChangeImage2(Image2) {
         var Image1 = $get('<%=btnImage1.ClientID%>')
         var tempURL = Image2.src;
         Image2.src = Image1.src;
         Image1.src = tempURL;
         return false;
     }
 </script>
 
 <tr>
         <td>
 <asp:ImageButton ID="btnImage1" runat="server" CausesValidation="false" ImageUrl="../Images/btnSave.png" OnClientClick = "JavaScript: return ChangeImage1(this);"
  />
 
  </td>
   <td>
   <asp:ImageButton ID="btnImage2" runat="server" CausesValidation="false" ImageUrl="../Images/btnUpdate.png" OnClientClick = "JavaScript: return ChangeImage2(this);"
  />
 
</td>
 </tr>
  Permalink  
Comments
KUFIKU 18-Jul-11 21:49pm
   
Thank you very much.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

I just solved it with foreach, thank you all for your replies.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160929.1 | Last Updated 18 Jul 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100