Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code:
XML
<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

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
 
Share this answer
 
Comments
Espen Harlinn 18-Jul-11 18:09pm    
Good reply - my 5 - maybe the script runs before the page is loaded ...
Sergey Alexandrovich Kryukov 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.
insead of img.ID use img.ClientID
 
Share this answer
 
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
if its a ImageButton then set img.ImageUrl property
 
Share this answer
 
Comments
KUFIKU 18-Jul-11 21:08pm    
It didn't work
here is the code. this works perfectly fine for me. do not add any code in codebehind file.

XML
<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>
 
Share this answer
 
Comments
KUFIKU 18-Jul-11 21:49pm    
Thank you very much.
I just solved it with foreach, thank you all for your replies.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900