0

I have a button on my html page that when I click it I would like it to asynchronously change a specific image on my page. So I have something like:

<a href="/test/page"> button </a>

And I would like that to change my image here:

<div id="container" style="display: none">
  <img src="/test/image.png" />
</div>

I think I'm suppose to use ajax here but I am very new to web development and I'm not sure, and suggestions would be appreciated.

Note: I would like this to happen without loading a new page, just reloading the image.

3
  • What do you want to change it to? If a server doesn't tell you what you need to change it to, you don't need Ajax. Commented Apr 17, 2012 at 13:49
  • How would the AJAX request determine what image to show? Commented Apr 17, 2012 at 13:49
  • It gets changed by the specific button that they click, so maybe i don't need ajax? Commented Apr 17, 2012 at 13:51

4 Answers 4

1
$('a').click(function(e){
    e.preventDefault();

    $('img').attr("src","newURL");
});
Sign up to request clarification or add additional context in comments.

3 Comments

e.preventDefault; won't do anything.
it prevents the A from actually clicking and navigating away from the page
@minitech you should be more clear and give the guy a hint :) eg "probably you've meant e.preventDefault();" ...
0

I would start with looking at the jquery and jquery-ui api documentation

http://api.jquery.com/jQuery.ajax/

http://jqueryui.com/demos/button/

what you want is a submit button that will make an ajax call and re-load a specific div.

The question about ajax is where are you getting the image from. If its from a back-end server you should use a ajax call. But if its all on the client you can just a make a normal java script call.

rough example of a button with an ajax call to some back end server

<script>
$(function() {
    $( "input:submit", ".demo" ).button();
    $( "a", ".demo" ).click(function() {
           $.ajax({
               url: "some url call to get image",
               data: " any data you want passed",
               success: function(html) {
                       $("#demo").html(html); // html you want reloaded for the div. 
              }
    });
});
</script>



<div class="demo">


<input type="submit" value="A submit button">



</div><!-- End demo -->

Comments

0

How about we don't use AJAX on this one?

HTML:

<a href="#" class="imageChanger" data-imgsrc="/link/new/image.jpg"> Press me </a>
<a href="#" class="imageChanger" data-imgsrc="/link/new/image23.jpg"> Press me too! </a>
<div id="container">
   <img src="lalala.jpg">
</div>

JS:

$(function() {
  $(document).on("click", ".imageChanger", function(e) {
    e.preventDefault();

    newSrc = $(this).data("imgsrc");

    $("#container").empty().append( '<img src=" ' + newSrc + ' ">' );

  });
});

This is typed from head so it might not work. I'll check it in the morning.

Comments

0
var counter = 1;
$(function() {
var imgArray = 
new Array("slide/1.jpg","slide/2.jpg","slide/3.jpg","slide/4.jpg");
$(document).on("click", "#btnSubmit", function(e) {
 e.preventDefault();
 if(counter>=imgArray.length)
 {
 counter=0;
 }
 newSrc = imgArray[counter++];//   $(this).data("imgsrc");
 $("#container").empty().append( '<img src=" ' + newSrc + ' " class="image-1-1"  width="100%" height="400">' );

});
});

html

<div id="container" class="coffee-span-8 column-1">
<img src="/slide/1.jpg" class="image-1-1"  width="100%" height="400" ></img>
 </div>
 <div class="coffee-span-6"><form METHOD="LINK" ACTION="index.html">    <button id="btnSubmit" type="submit">next</button></FORM>
 </div>

1 Comment

Why is Flash being used here? Where was this in the original question?

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.