Moshe
Moshe

Reputation: 2684

using jQuery for buttons/ajax requests

I am fairly new to jQuery, and even though there are a lot of tutorials on how to bind to buttons, I believe my set up is a little more complicated (beyond the scope).

What I have:

-I am using Django to populate my Makes on a view.

Here is my template:

    <div class="container">
    <div class="row">
    <div class="btn-group" id="makesTable">
    {%  if makes %}
    {% for make in makes %}
    <button  type="button" name="button" class="btn btn-default" id="{{ make.id }}">
        <br />
        <img class="center-block" src="[REDACTED]"  />
        <br />
        <p>{{ make.name }}</p>
    </button>
    {% endfor %}
{% endif %}
</div>
</div>
</div>

I am currently having an issue with responsive design. I would like for the buttons to be arranged in a 5x7 grid, however, sometimes, I get the following issue:

This is how it looks when everything is good!

This is the problem. Notice the spacing with a ?

The Workflow:

Sample Response:

{
"name": "BMW",
"model": [
    {
        "name": "2 Series",
    },
    {
        "name": "3 Series",
    }]
 }

This is for a single page app in a sense where no page refresh is necessary with the use of JSON and AJAX.

Full disclosure: I am an entry level programmer/web developer.

code snippet:

<div class="main-content container-fluid"> 
 <div class="container">
   <div class="row">
     <div class="btn-group" id="makesTable">
        <button  type="button" name="button" class="btn btn-default" id="200002038">
        <br />
        <img class="center-block" src=" "  />
        <br />
        <p>Acura</p>
    </button>

    <button  type="button" name="button" class="btn btn-default" id="200464140">
        <br />
        <img class="center-block" src=""  />
        <br />
        <p>Alfa Romero</p>
    </button>

<script>
  $(document).ready(function() {
   $(document).on( "click", "button", function() {
     alert($(this).attr('id')); //testing functionality
    });
 });
</script>
<script src="assets/lib/jquery/jquery.min.js" type="text/javascript"></script>
<script src="assets/lib/perfect-scrollbar/js/perfect-scrollbar.jquery.min.js" type="text/javascript"></script>
<script src="assets/js/main.js" type="text/javascript"></script>
<script src="assets/lib/bootstrap/dist/js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
    //initialize the javascript
    App.init();
  });

</script>
</div>
 </div>
  </div>
   </div>

Upvotes: 0

Views: 111

Answers (3)

Brian Ocampo
Brian Ocampo

Reputation: 1515

First of all, event association to buttons should be better via classes (it's a better practice).For the javascript click code check the other answers. For ajax requests, you can refer to this links:

https://api.jquery.com/jquery.post/

https://api.jquery.com/jquery.get/

https://api.jquery.com/jquery.when/

By example 'when' function, it allows you to know when request ended and execute any actions after.

Upvotes: 2

Mohammed Elshennawy
Mohammed Elshennawy

Reputation: 967

from your replied comment I expect that you attach the handler before the button already rendered so I recommend you to use bubbling event concept like this:-

$(document).ready(function() {
   $(document).on( "click", "button", function() {
      alert($(this).attr('id')); //testing functionality
   });
});

and the the alert will pop up and you can do whatever else you want to do.

Upvotes: 1

Sumama Waheed
Sumama Waheed

Reputation: 3609

Since the buttons are dynamically generated. You have to make the handler like this:

$(document).on('click', 'button.btn', function(e) {   
    alert($(this).attr('id')); //testing functionality
});

Upvotes: 2

Related Questions