0

I am trying to show a booking form when I click the button using jquery toggle.

HTML

foreach($result['apiAvailableBuses'] as $value){?>
    <tr>
</td>
        <td><?php echo $value['fare']; ?></td>
        <td>

            <?php echo $value['availableSeats'].'&nbsp;Seats'; ?>&nbsp;&nbsp;
            <input type="button" id="<?php echo $i; ?>" class="btn btn-primary" value="Book" name="Book">
        </td>
    </tr>
<tr class="main">
        <td colspan="5">
            <div class="well" id="result<?php echo $i; ?>">Booking form here</div>
        </td>
    </tr>
<?php $i++; } ?>

Script

$(".main").hide();
$(".btn-primary").click(function(){
        var id=$(this).attr('id');
        $('#result'+id).toggle();
    });

But when I click on the button I can't see the booking form div

5
  • Its wrapped in ready function Commented Dec 29, 2015 at 4:16
  • 2
    You also have a closing </td> tag right after the opening <tr> tag .. Commented Dec 29, 2015 at 4:17
  • what is $i, it's not declared anywhere Commented Dec 29, 2015 at 4:21
  • @Disha i've declared it. Commented Dec 29, 2015 at 4:22
  • try passing $('#result'+id).toggle(true); Commented Dec 29, 2015 at 4:34

2 Answers 2

2

In your script, you are hiding the parent tr using $(".main").hide(); and hence toggle of the child element is not working. To make it work you have to make visible the parent element and then toggle the div.

$(".main").hide();
$(".btn-primary").click(function(){
       $(".main").show();
    var id=$(this).attr('id');
    $('#result'+id).toggle();
});

Now it should work.

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

5 Comments

it displays all the divs
@Blessan, please modify the selector as per your requirement to select the correct tr element to show and not all tr element...
$('#result'+id).closest('tr.main').toggle(); might work?
Again select all the divs
@BlessanKurien, remove $(".main").show(); and replace $('#result'+id).toggle(); with $('#result'+id).closest('tr.main').toggle();
0

Try using .on()

$(document).ready(function(){
$(document).on("click",".btn-primary",function(){
    var id=$(this).attr('id');
    $('#result'+id).toggle();
});
});

UPDATE

Another thing I want to explain. You are clicking on a button and then getting its ID and then doing .toggle() with it.

I think you can do this way also.

$(document).ready(function(){
$(document).on("click",".btn-primary",function(){
    //var id=$(this).attr('id');
    //$('#result'+id).toggle();
    $(this).closest("tr").next("tr").find(".well").toggle();
});
});

5 Comments

remove unwanted </td> from third line of your HTML code.
Nop ,actually its only a small portion of the code a full HTML table is included
Yeah its wroked,but the inital load all the pages display
You can hide in on page load by adding $(".main").hide();
when $(".main").hide() using this the first proble is occures

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.