1

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.

1 Answer 1

1

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

$("#" + seats[i].seatnum).attr("src", "images/taken.gif");
Sign up to request clarification or add additional context in comments.

1 Comment

that has sorted my problem, thank you very much, i have litterally spent 5 hours looking at this problem and probably would have spent another 5 umming and arring before giving in.

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.