Step
Step

Reputation: 53

javascript, changing an image source based on a sql database

i am writing javascript to change the image source based on the results of a database. the database is returning the id's of objects set to 0 and that seems to be working as it is passing along the right id. now i am trying to match what id's have been sent back with the id in my html page so that i can change the source image for the html id to something else. eg A1 has been sent back now the td id A1 on my html page needs to change its image. so far i have ;

function getSeats() {  // the page load function
    var myurl="scripts/getseats.php";
    $.ajax({
        type:"GET",
        url: myurl, dataType:'json', // a JSON object will be returned from the server.
        success: function(seats){ // it works - we have the data!
            for(var i=0;i<seats.length;i++){
                alert(seats[i].seatnum);// this has confirmed that it is sending back the correct seatnum that i have purposely set to 0.
                $("#.seats[i].seatnum").attr("src","images/taken.gif");

i have tried all sorts but because i am pretty new to javascript i dont really know where i am going wrong so i appologise if it may seem obvious.

Upvotes: 1

Views: 390

Answers (1)

Barmar
Barmar

Reputation: 781058

Javascript doesn't expand variables inside strings, you have to use explicit concatenation:

$("#" + seats[i].seatnum).attr("src", "images/taken.gif");

Upvotes: 1

Related Questions