1

I have a jQuery script to change background image with respect to ajax call back as below;

<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
      $cell1 = $('#res1');
      $cell2 = $('#res2');
      $.ajax({
        type: "POST",
        url: "ajax.php",
        data: {
          filename: $("#title1").html()
        },
        success: function(response){
          $cell1.css("background-image", "url('pdfthumb/" + response + ".jpg')");
        }
      });
      $.ajax({
        type: "POST",
        url: "ajax.php",
        data: {
          filename: $("#title2").html()
        },
        success: function(response){
          $cell2.css("background-image", "url('pdfthumb/" + response + ".jpg')");
        }
      });

    });
</script>

I have $cell1, $cell2, #cell3... and much more.. and here only 2 is shown. But each time, when I want to change background, I have to call ajax and this made my script very lengthy. The way I believe to trim the code is to make a custom jQuery function for ajax request and change background.

How can I do this?

2
  • are you changing the background image based on the image selected and uploaded by the user??? Commented Jun 7, 2011 at 13:49
  • no.. something related with server files.. Commented Jun 7, 2011 at 13:56

4 Answers 4

1

What does your HTML look like?

Assuming you have something like:

<div id="res1" class="cell">
    <h2 id="title1" class="title">Title</h2>
    <p>Some content</p>
</div>

<div id="res2" class="cell">
    <h2 id="title2" class="title">Title</h2>
    <p>Some content</p>
</div>

You can do:

$(document).ready(function(){
    $('.cell').each(function(){
        cell = $(this);
        $.post('ajax.php',{filename: $('h2.title', cell).html()}, function(){
            cell.css("background-image", "url('pdfthumb/" + response + ".jpg')");
        });
    });
});

The benefit of doing it this way is it doesnt matter what your cell IDs are. You dont have to try to loop through a concrete set of numbers.

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

1 Comment

upvoting your answer, as this is cleaner
0

Something like:

function updateBackground(cellId, titleId) {
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: {
          filename: $("#"+titleId).html()
        },
        success: function(response){
          $("#"+cellId).css("background-image", "url('pdfthumb/" + response + ".jpg')");
        }
    });
}

$(document).ready(function(){
    updateBackground("res1", "title1");
    updateBackground("res2", "title2");
});

1 Comment

This still requires that you specifically name every single cell and title in your document.ready. What if you add one or remove one from the HTML, and forget to update the javascript? It will throw an error because that cell wont be found.
0

What you can do is this:

changeBackground = function(response, num)
{
     $('#res'+num).css("background-image","url('pdfthumb/" + response + ".jpg')");
}

for(var i=0;i < maxNum ; i++ )
{
     $.ajax({
        type: "POST",
        url: "ajax.php",
        data: {
          filename: $("#title"+i).html()
        },
        success: function(response){
               changeBackground(response,i)
        }
      });
}

The changebackground is your custom function, and you make multiple ajax calls here too, but this is in a loop. maxNum is the number of times/dom object you want to change the background for.

NOTE: the objects should be names accordingly, this script is specifically for the templates you have defined here (i.e title1,title2 etc, res1, res2 etc should be names as they have been in your example script)

3 Comments

so how it will be if i have a specific number say 6 cells (title1-title6)
There are easier ways with jQuery than to hardcode the number of cells, or hardcode the cell titles. The answer the OP accepted may work, but its still messy.
yes I know that, making them all belong to a class, or even using the partial name selector could have done the trick, but the OP already had a way of approaching the issue, and I simply told him how to achieve it.
0

One way to optimize your code is combine your request and response. This way you will get all the background images in one request.

2 Comments

how?? i dont know how to do that.. can u post??
This would require the server-side script to return the specific changes, only client side will not suffice for this specific solution

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.