2

How to use variable "hotel" after load step-02.php file?

    $(function () {
    var hotel = [];
    $('#forPrice').on('click', function() {
        $('select').each(function() {
            if($(this).val() > 0 && $(this).val() < 10) {
                var room = {};
                room["room"] = $(this).attr('room');
                room["price"] = $(this).attr('price');
                room["val"] = $(this).val();
                room["html"] = $(this).parents(".selectRowRoom").html();

                hotel.push(room);
            }
        });
        console.log(hotel);
    });

    $('#forPrice').click(function () {
        $('#Step-01').hide();
        $('#Step-02').show();
        $('.hiddenAndShow').hide();
        $( "#Step-02" ).load( 'step_02.php' );
    });
})

I need

room["html"] = $(this).parents(".selectRowRoom").html();

to show after loading step-02.php.

step-02.php:

<div class="thisRoom">
    // Insert room["html"] = $(this).parents(".selectRowRoom").html();
</div>
9
  • where are you loading php file? Commented Oct 8, 2016 at 18:24
  • @xzegga Hi, I got this question correct. Commented Oct 8, 2016 at 18:35
  • So are you trying to tell us .thisRoom is in step_02.php or what ? Commented Oct 8, 2016 at 18:35
  • @adeneo yes, insert in .thisRoom in step-02.php Commented Oct 8, 2016 at 18:44
  • jsfiddle.net/0rh9qwm0 Commented Oct 8, 2016 at 18:45

2 Answers 2

1

jQuery load() is a shortcut for $.ajax, which is asynchronous, you have to wait for the result to be able to access it.

Luckily load() has a callback that fires when the content is inserted and ready for accessing

$(function() {
    var hotel = [];
    $('#forPrice').on('click', function() {
        $('select').each(function() {
            if ($(this).val() > 0 && $(this).val() < 10) {
                var room = {};
                room["room"] = $(this).attr('room');
                room["price"] = $(this).attr('price');
                room["val"] = $(this).val();
                room["html"] = $(this).parents(".selectRowRoom").html();

                hotel.push(room);
            }
        });
        console.log(hotel);
    });

    $('#forPrice').click(function() {
        $('#Step-01').hide();
        $('#Step-02').show();
        $('.hiddenAndShow').hide();
        $("#Step-02").load('step_02.php', function() { // callback here
            $("#Step-02").find('.thisRoom').html(JSON.stringify(hotel));
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Make hotel global and save key number of room in data attr. Like this :

$(function () {
    window.hotel = []; // new
    $('#forPrice').on('click', function() {
        $('select').each(function() {
            if($(this).val() > 0 && $(this).val() < 10) {
                var room = {};
                room["room"] = $(this).attr('room');
                room["price"] = $(this).attr('price');
                room["val"] = $(this).val();
                room["html"] = $(this).parents(".selectRowRoom").html();

                $(this).data("numRoom", hotel.length); // new
                hotel.push(room);
            }
        });
        console.log(hotel);
    });

    $('#forPrice').click(function () {
        $('#Step-01').hide();
        $('#Step-02').show();
        $('.hiddenAndShow').hide();
        $( "#Step-02" ).load( 'step_02' );
    });
})

//After :
hotel[$(this).data("numRoom")]["html"] = $(this).parents(".selectRowRoom").html();

Comments

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.