There is such a function:

var getInfoForPrices = function (arg) { var dateIn = $(".date-in").val(); var dateOut = $(".date-out").val(); var roomsId = $('.collect-date .rooms-id').val(); var roomsCount = $('.collect-date .rooms-count').val(); var roomsIdArray = roomsId.split(","); var roomsCountArray = roomsCount.split(","); $('.collect-date .start-date').val(dateIn); $('.collect-date .end-date').val(dateOut); if (arg.hasClass('room-count')) { var currentRoomId = arg.attr('id').split('-')[1]; var currentRoomCount = arg.find('option:selected').val(); if($.inArray(currentRoomId, roomsIdArray) === -1){ roomsIdArray.push(currentRoomId); roomsCountArray.push(currentRoomCount); } else { var elPos = $.inArray(currentRoomId, roomsIdArray); roomsCountArray[elPos] = currentRoomCount; } $('.collect-date .rooms-id').val(roomsIdArray); $('.collect-date .rooms-count').val(roomsCountArray); } }; 

which serves to collect data and substitute them into hidden fields. The function is called when several fields change in another form:

 $(".date-in, .date-out, .room-count").change(function () { getInfoForPrices($(this)); }); 

But it turns out that the first elements of the arrays are empty each time.

alt text

How to make sure that the first empty element is not entered into the array?

    1 answer 1

    In general, done so

     var getInfoForPrices = function (arg) { var dateIn = $(".date-in").val(); var dateOut = $(".date-out").val(); $('.collect-date .start-date').val(dateIn); $('.collect-date .end-date').val(dateOut); if (arg.hasClass('room-count')) { var roomsId = $('.collect-date .rooms-id').val(); var roomsCount = $('.collect-date .rooms-count').val(); var roomsIdArray = []; if (roomsId.length > 0) { roomsIdArray = roomsId.split(","); } var roomsCountArray = []; if (roomsCount.length > 0) { roomsCountArray = roomsCount.split(","); } var currentRoomId = arg.attr('id').split('-')[1]; var currentRoomCount = arg.find('option:selected').val(); if($.inArray(currentRoomId, roomsIdArray) === -1){ roomsIdArray.push(currentRoomId); roomsCountArray.push(currentRoomCount); } else { var elPos = $.inArray(currentRoomId, roomsIdArray); roomsCountArray[elPos] = currentRoomCount; } $('.collect-date .rooms-id').val(roomsIdArray); $('.collect-date .rooms-count').val(roomsCountArray); } }; 
    • Well. Also zaminusovali. It would be better explained how to properly do. - Heidel
    • Zaminusovali for a bad answer. Themselves asked a question, they themselves answered it badly: without an explanation of what was done and what was not working. Useless question-answer. - MasterAlex
    • what did not work was written in detail in the original question. According to the answer: the code is not enough what was done? there is still not such a long sheet that it was difficult to see what has changed. - Heidel