1

I got this jQuery script:

function changeImage(e) {
    var nextImg = $(e).attr('nextImage');
    var oldImg = document.getElementById('<%=imgMain.ClientID %>');
    $(oldImg).attr('src', nextImg);

    return false;
}

and this ASP.NET markup:

<asp:LinkButton runat="server" id="lbOne" Text="1" OnClientClick="changeImage(this); return false;" />
<asp:LinkButton runat="server" id="lbTwo" Text="2" OnClientClick="changeImage(this); return false;" />

Code behind:

lbOne.Attributes.Add("nextImage", "some_image");
lbTwo.Attributes.Add("nextImage", "some_image2);

some how it wont change the image src the jQuery script looks okay to me any ideas?

Found the error:

var oldImg = document.getElementById('<%=imgMain.ClientID %>');

need to add '#' like this:

var oldImg = document.getElementById('#<%=imgMain.ClientID %>');

works now. thanks for the comments!

3
  • still wont work i checked the source of the page and looked at the liked button it has href:href="javascript:__doPostBack('id','') maybe its because of this? it throws post back? Commented Jul 24, 2011 at 7:51
  • I'm not sure you can "override" the client side code of LinkButton.. why not use ordinary <a> tags for this? Commented Jul 24, 2011 at 8:11
  • Can you mark the question as answered so everyone knows? Commented Jul 24, 2011 at 12:02

2 Answers 2

1

Try making a small adjustment so that the postback is completely blocked.

<asp:LinkButton runat="server" id="lbTwo" Text="2" OnClientClick="return changeImage(this);" />
Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me when I tested it, but your sample code is missing a qoute after "some_image2 and is missing the imgMain control.

Have you checked in firebug that the JavaScript is firing? and are you sure your page has a reference to jQuery?

6 Comments

the quote isnt the problem i just change the image name when i posted the code here probably just missed the quote and imgMain is an asp:Image: <web:Image runat="server" id="imgMain" />
what is a <web:image control is it not asp:image? or are you using some other add-on to asp.net?
private library same as asp:Image with diffrent options
have you debugged the javascript in firebug? to see if the event is firing and the client ids are pickedup correctly?
found the error it should be like that in the jQuery: var oldImg = $('#<%=imgMain.ClientID %>'); forgot the '#' basic...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.