1

How can i change the image of an image button clientside, on click.

I have tried this:

function changetoDownload(theId) {
   $(theId.id).delay(2000)
   .queue(function (next) { $(theId).attr("ImageURL", "Images/Apps/free.png"); next(); });
}

<asp:ImageButton ID="ImageButton1" OnClick="Button7_Click" OnClientClick="changetoDownload(this.id);" ImageUrl="Images/download.gif" runat="server" />

3 Answers 3

1
//target all `<input type="image" />` elements and bind a `click` event handler to them
$('input[type="image"]').on('click', function () {

    //change the `src` of this element
    $(this).attr('src', 'Images/Apps/free.png');
});

Note that .on() is new as of jQuery 1.7 and in this case is the same as .bind().

I'm not sure how ASP.net will render your button but you can target it using the ID of the element:

$('#MainContent_ImageButton1').on('click', function () {

    //change the `src` of this element
    $(this).attr('src', 'Images/Apps/free.png');
});

You can check the source of your document in a browser to see if that is the actual rendered ID of the ASP button.

Here is a demo: http://jsfiddle.net/jasper/PQ4fp/

Sign up to request clarification or add additional context in comments.

11 Comments

OP is using master page as told by him in comments. So I think, ID is not traceable in ur case.
$('#MainContent_ImageButton1').on('click', function () { //change the src of this element $(this).attr('src', 'Images/Apps/free.png'); });
OP posted the HTML source of the element and it's an input tag with the ID of MainContent_ImageButton1. So either of my selectors above should work (unless the ID may be changed by the framework, that I'm not sure about).
the above i posted in comment are working, when using MainContent_ImageButton1 as ID. BUT can i do that with multiple buttons? without making a function for each ImageButton?
@ClemenGronver In that case you should either add a class to the elements to group them together so you can select them like this: $('.my-custom-class').on('click', function () { ... });, or you can use the first selector in my answer $('input[type="image"]') which selects all the <input type="image" /> elements (like your button). You can also select multiple elements at the same time separating the selectors by commas: $('#MainContent_ImageButton1, #MainContent_ImageButton2, #MainContent_ImageButton3')...
|
1

It is very easy in JavaScript

HTML Code

<asp:ImageButton ID="img" runat="server"  OnClientClick="return abc(this);" />

JavaScript Code

<script language="javascript" type="text/javascript">
function abc(ImageID) {
    var img = document.getElementById(ImageID.id);
    img.src = "Jellyfish.jpg";
    return false;
}

</script>

3 Comments

Since jQuery is being used, you might as well take advantage of the easy binding system rather than using inline JS, which can be a nightmare to maintain since you may have to update many pieces of code rather than a single piece of code that does all your event handling.
I can keep this function in JavaScript file and same can be reused. FYI, Please check the Tags and Title.
I have code similar to the above but, while I can see that it is indeed changing the src in the debugger, the old image continues to be displayed. Something more is required it seems :(
1

JQuery runs at client side. The reason your javascript isn't working is because the "asp" tag is rendered server side and once displayed on the client side the markup changes to HTML. You are referencing the ImageURL attribute which doesn't exist in HTML it would be the "src" attribute. You can check this by deploying your code and then using "View Source" in your browser.

3 Comments

View source shows that its rendered as: <input type="image" name="ctl00$MainContent$ImageButton1" id="MainContent_ImageButton1" src="Images/download.gif" onclick="changetoDownload(this.id);" /
From there you can see that the image link is being rendered as "src". Jasper above has the correct solution and from your post you can see that the ID is MainContent_ImageButton1 so just put that in place of ImageButton1 in the code example above.
I up ticked this because it is true and contains useful, relevant information but alas it is not sufficient for me. I can see the src attribute and innerHTML fields are correctly updated in the browser debugger but the old image continues to be displayed! :(

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.