title: how to pass data in jquery from an if condition blade file
body: <p>i have 2 types of a product, one with attributes and a product without attributes.i want to add product to cart in the listing page using jquery such that on click on the add to cart button the product is just added to cart table.for the product with attributes the user will have to select an attribute but for the product with attributes it will just add to cart.</p>
  <pre><code>&lt;div class=&quot;fs-sm mb-4&quot;&gt;
   &lt;input name=&quot;quantity&quot; type=&quot;number&quot; value=&quot;1&quot; class=&quot;prod_qty&quot; &gt;
    &lt;input class=&quot;prod_id&lt;?php echo $countp;?&gt;&quot; name=&quot;product_id&quot; type=&quot;hidden&quot; value=&quot;{{ $product-&gt;id }}&quot;&gt;
      @if ($product-&gt;is_attribute==0)
        &lt;input class=&quot;prodsize&quot; name=&quot;productattrsize&quot; type=&quot;text&quot; value=&quot;small&quot;&gt;
        &lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;
        &lt;div class=&quot;d-flex flex-row&quot;&gt;
           &lt;?php $discountedprice=Merchadise::getdiscountedprice($product-&gt;id);?&gt;
           &lt;!-- Product price--&gt;
           @if ($discountedprice&gt;0)
              &lt;del&gt;&lt;p class=&quot;lead text-muted text-decoration-line-through&quot;&gt;sh {{$product-&gt;merch_price }}&lt;/p&gt;&lt;/del&gt;
              &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; style=&quot;float-right&quot;&gt;sh {{ $discountedprice }}&lt;/p&gt;
           @else
               &lt;p class=&quot;lead text-muted text-decoration-line-through&quot;&gt;sh {{$product-&gt;merch_price }}&lt;/p&gt;
           @endif
         &lt;/div&gt;
       &lt;/div&gt;
                      @elseif ($product-&gt;is_attribute==1)
                          &lt;select class=&quot;custom-select prodsize&quot; productid=&quot;{{ $product-&gt;id }}&quot; name=&quot;productattrsize&quot;&gt;
                              &lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;
                              @foreach ($product-&gt;merchadiseattributes as $attribute )
                                  &lt;option value=&quot;{{ $attribute-&gt;productattr_size }}&quot; required&gt;{{ $attribute-&gt;productattr_size }}&lt;/option&gt;
                              @endforeach
                          &lt;/select&gt;
                          &lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;
                              &lt;div class=&quot;d-flex flex-row&quot;&gt;
                                  &lt;?php $discountedprice=Merchadise::getdiscountedprice($product-&gt;id);?&gt;
                                  &lt;!-- Product price--&gt;
                                  @if ($discountedprice&gt;0)
                                      &lt;del&gt;
                                          &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; id=&quot;showattrprice&quot;&gt;sh {{$product-&gt;merch_price }}&lt;/p&gt;
                                      &lt;/del&gt;
                                      &lt;p class=&quot;lead text-muted text-decoration-line-through ml-3&quot; id=&quot;showcalculatedattrprice&quot; style=&quot;float-right&quot;&gt;sh {{ $discountedprice }}&lt;/p&gt;
                                  @endif
                              &lt;/div&gt;
                          &lt;/div&gt;
                      @endif
                      &lt;div class=&quot;d-flex &quot;&gt;
                          &lt;a href=&quot;{{ url('merchadise/'.Str::slug($product-&gt;merch_name).'/'.$product-&gt;id) }}&quot; class=&quot;p-3 bg-dark text-white btn btn-primary ml-2&quot;&gt;View Product&lt;/a&gt;
                          &lt;a class=&quot;p-3 bg-dark text-white btn btn-primary ml-2 add-to-cart&lt;?php echo $countp;?&gt;&quot;&gt;Add to Cart&lt;/a&gt;
                          &lt;div id=&quot;successmsg&lt;?php echo $countp;?&gt;&quot; class=&quot;alert alert-message bg-grey text-dark&quot;&gt;&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  </code></pre>
  <p>the case here is for the product without attribute the size will be passed as small but for the dropdown the selected option will be selected.the issue am getting here is my jquery code is only selecting on the first attribute option that i have selected.also its not grabbing the size for the products without attributes,this one ..this is my jquery code.</p>
  <pre><code>            $('.add-to-cart').click(function(e) { e.preventDefault();
              var prod_id=$(this).closest('.ps-shop').find('.prod_id&lt;?php echo $i;?&gt;').val();
              var prod_qty=$(this).closest('.ps-shop').find('.prod_qty').val();
              var prod_size=$(this).closest('.ps-shop').find('.prodsize').val();
              alert(prod_size)
              if(prod_size==&quot;&quot;){
                  alert(&quot;Please Select An Attribute&quot;);
                  return false;
              }
              $.ajax({
                  method:&quot;post&quot;,
                  url:&quot;add_to_cart&quot;,
                  data:{
                      'productattrsize':prod_size,
                      'product_id':prod_id&lt;?php echo $i;?&gt;,
                      'quantity':prod_qty,
                  },
                  success:function(response){
                      // console.log(response)
                      $('.add-to-cart&lt;?php echo $i;?&gt;').hide();
                      $('#successmsg&lt;?php echo $i;?&gt;').show();
                      $('#successmsg&lt;?php echo $i;?&gt;').append('product has been added to cart');
                  }
              });
          });
  </code></pre>
  <p>this is how the html code looks like in the browser</p>
  <pre><code>    &lt;div class=&quot;row&quot;&gt;
       &lt;div class=&quot;col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1 mb-5&quot; style=&quot;border: 2px solid green&quot;&gt;
          &lt;div class=&quot;card&quot;&gt; 
            &lt;img class=&quot;card-img-top&quot; src=&quot;http://djvoskill/images/productimages/small/Merchadise 2 With Attributes-5074.PNG&quot;&gt;
              &lt;div class=&quot;card-body&quot;&gt;
                &lt;h6 class=&quot;font-weight-bold pt-1&quot;&gt;5.Merchadise 2 With Attributes&lt;/h6&gt;
                &lt;div class=&quot;d-flex align-content-center justify-content-center&quot;&gt;
                  &lt;span class=&quot;pt-1&quot;&gt;Tshirts&lt;/span&gt;
                &lt;/div&gt;
                  1.&lt;span class=&quot;pt-1&quot;&gt;cotton&lt;/span&gt;
                          &lt;hr&gt;
                          2.&lt;span class=&quot;pt-1&quot;&gt;wedding&lt;/span&gt;
                          &lt;div class=&quot;fs-sm mb-4&quot;&gt;
                              &lt;input name=&quot;quantity&quot; type=&quot;number&quot; value=&quot;1&quot; class=&quot;prod_qty&quot;&gt;
   &lt;input class=&quot;prod_id0&quot; name=&quot;product_id&quot; type=&quot;hidden&quot; value=&quot;5&quot;&gt;
  &lt;select class=&quot;custom-select prodsize&quot; productid=&quot;5&quot; name=&quot;productattrsize&quot;&gt;
   &lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;                                                            
    &lt;option value=&quot;Green&quot; required=&quot;&quot;&gt;Green&lt;/option&gt;                            
    &lt;option value=&quot;Black&quot; required=&quot;&quot;&gt;Black&lt;/option&gt;                           
    &lt;option value=&quot;Blue&quot; required=&quot;&quot;&gt;Blue&lt;/option&gt;                         &lt;/select&gt;
  &lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;&lt;div class=&quot;d-flex flex-row&quot;&gt;                                                      &lt;!-- Product price--&gt;                                                  &lt;del&gt;
                                                  &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; id=&quot;showattrprice&quot;&gt;sh 300&lt;/p&gt;
                                              &lt;/del&gt;
                                              &lt;p class=&quot;lead text-muted text-decoration-line-through ml-3&quot; id=&quot;showcalculatedattrprice&quot; style=&quot;float-right&quot;&gt;sh 270&lt;/p&gt;
                                                                              &lt;/div&gt;&lt;/div&gt;
    &lt;div class=&quot;d-flex &quot;&gt;
       &lt;a href=&quot;http://djvoskill/merchadise/merchadise-2-with-attributes/5&quot; class=&quot;p-3 bg-dark text-white btn btn-primary ml-2&quot;&gt;View Product&lt;/a&gt;
                                  &lt;a class=&quot;p-3 bg-dark text-white btn btn-primary ml-2 add-to-cart0&quot;&gt;Add to Cart&lt;/a&gt;
                                  &lt;div id=&quot;successmsg0&quot; class=&quot;alert alert-message bg-grey text-dark&quot; style=&quot;display: none;&quot;&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                                  &lt;div class=&quot;col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1 mb-5&quot; style=&quot;border: 2px solid green&quot;&gt;
                  &lt;div class=&quot;card&quot;&gt; 
                      &lt;img class=&quot;card-img-top&quot; src=&quot;http://djvoskill/images/productimages/small/Product 1 with No Attributes-667.PNG&quot;&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
                          &lt;h6 class=&quot;font-weight-bold pt-1&quot;&gt;4.d.Merchadise 1 with Attributes&lt;/h6&gt;
                          &lt;div class=&quot;d-flex align-content-center justify-content-center&quot;&gt;
                              &lt;span class=&quot;pt-1&quot;&gt;Tshirts&lt;/span&gt;
                          &lt;/div&gt;
                          1.&lt;span class=&quot;pt-1&quot;&gt;leather&lt;/span&gt;
                          &lt;hr&gt;
                          2.&lt;span class=&quot;pt-1&quot;&gt;casual&lt;/span&gt;
                          &lt;div class=&quot;fs-sm mb-4&quot;&gt;
                              &lt;input name=&quot;quantity&quot; type=&quot;number&quot; value=&quot;1&quot; class=&quot;prod_qty&quot;&gt;
                              &lt;input class=&quot;prod_id1&quot; name=&quot;product_id&quot; type=&quot;hidden&quot; value=&quot;4&quot;&gt;
  &lt;select class=&quot;custom-select prodsize&quot; productid=&quot;4&quot; name=&quot;productattrsize&quot;&gt;
                                      &lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;                                       &lt;option value=&quot;Small&quot; required=&quot;&quot;&gt;Small&lt;/option&gt;                         &lt;option value=&quot;Medium&quot; required=&quot;&quot;&gt;Medium&lt;/option&gt;                   &lt;/select&gt;&lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;&lt;div class=&quot;d-flex flex-row&quot;&gt;
                                                                                  &lt;!-- Product price--&gt;
                                                                                      &lt;del&gt;
                                                  &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; id=&quot;showattrprice&quot;&gt;sh 200&lt;/p&gt;
                                              &lt;/del&gt;
                                              &lt;p class=&quot;lead text-muted text-decoration-line-through ml-3&quot; id=&quot;showcalculatedattrprice&quot; style=&quot;float-right&quot;&gt;sh 180&lt;/p&gt;                                                                            &lt;/div&gt;&lt;/div&gt;
  &lt;div class=&quot;d-flex &quot;&gt;
                                  &lt;a href=&quot;http://djvoskill/merchadise/dmerchadise-1-with-attributes/4&quot; class=&quot;p-3 bg-dark text-white btn btn-primary ml-2&quot;&gt;View Product&lt;/a&gt;
                                  &lt;a class=&quot;p-3 bg-dark text-white btn btn-primary ml-2 add-to-cart1&quot;&gt;Add to Cart&lt;/a&gt;
                                  &lt;div id=&quot;successmsg1&quot; class=&quot;alert alert-message bg-grey text-dark&quot; style=&quot;display: none;&quot;&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                                  &lt;div class=&quot;col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1 mb-5&quot; style=&quot;border: 2px solid green&quot;&gt;
                  &lt;div class=&quot;card&quot;&gt; 
                      &lt;img class=&quot;card-img-top&quot; src=&quot;http://djvoskill/images/productimages/small/Merchadise 2 NoAttributes-7262.jpg&quot;&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
                          &lt;h6 class=&quot;font-weight-bold pt-1&quot;&gt;3.c.Merchadise 2 NoAttributes&lt;/h6&gt;
                          &lt;div class=&quot;d-flex align-content-center justify-content-center&quot;&gt;
                              &lt;span class=&quot;pt-1&quot;&gt;Tshirts&lt;/span&gt;
                          &lt;/div&gt;
                          1.&lt;span class=&quot;pt-1&quot;&gt;woolen&lt;/span&gt;
                          &lt;hr&gt;
                          2.&lt;span class=&quot;pt-1&quot;&gt;casual&lt;/span&gt;
                          &lt;div class=&quot;fs-sm mb-4&quot;&gt;
                              &lt;input name=&quot;quantity&quot; type=&quot;number&quot; value=&quot;1&quot; class=&quot;prod_qty&quot;&gt;
                              &lt;input class=&quot;prod_id2&quot; name=&quot;product_id&quot; type=&quot;hidden&quot; value=&quot;3&quot;&gt;
  &lt;input class=&quot;prodsize&quot; name=&quot;productattrsize&quot; type=&quot;text&quot; value=&quot;small&quot;&gt;
                                  &lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;
                                      &lt;div class=&quot;d-flex flex-row&quot;&gt;
                                                                                  &lt;!-- Product price--&gt;
                                                                                      &lt;del&gt;
                                                  &lt;p class=&quot;lead text-muted text-decoration-line-through&quot;&gt;sh 301&lt;/p&gt;
                                              &lt;/del&gt;
                                              &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; style=&quot;float-right&quot;&gt;sh 204.68&lt;/p&gt;
                                                                              &lt;/div&gt;
                                  &lt;/div&gt;
                      
                                                          
                              &lt;div class=&quot;d-flex &quot;&gt;
                                  &lt;a href=&quot;http://djvoskill/merchadise/cmerchadise-2-noattributes/3&quot; class=&quot;p-3 bg-dark text-white btn btn-primary ml-2&quot;&gt;View Product&lt;/a&gt;
                                  &lt;a class=&quot;p-3 bg-dark text-white btn btn-primary ml-2 add-to-cart2&quot;&gt;Add to Cart&lt;/a&gt;
                                  &lt;div id=&quot;successmsg2&quot; class=&quot;alert alert-message bg-grey text-dark&quot; style=&quot;display: none;&quot;&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                                  &lt;div class=&quot;col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1 mb-5&quot; style=&quot;border: 2px solid green&quot;&gt;
                  &lt;div class=&quot;card&quot;&gt; 
                      &lt;img class=&quot;card-img-top&quot; src=&quot;http://djvoskill/images/productimages/small/Merchadise 2 NoAttributes-2100.jpg&quot;&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
                          &lt;h6 class=&quot;font-weight-bold pt-1&quot;&gt;2.b.Merchadise 2 NoAttributes&lt;/h6&gt;
                          &lt;div class=&quot;d-flex align-content-center justify-content-center&quot;&gt;
                              &lt;span class=&quot;pt-1&quot;&gt;Tshirts&lt;/span&gt;
                          &lt;/div&gt;
                          1.&lt;span class=&quot;pt-1&quot;&gt;woolen&lt;/span&gt;
                          &lt;hr&gt;
                          2.&lt;span class=&quot;pt-1&quot;&gt;wedding&lt;/span&gt;
                          &lt;div class=&quot;fs-sm mb-4&quot;&gt;
                              &lt;input name=&quot;quantity&quot; type=&quot;number&quot; value=&quot;1&quot; class=&quot;prod_qty&quot;&gt;
                              &lt;input class=&quot;prod_id3&quot; name=&quot;product_id&quot; type=&quot;hidden&quot; value=&quot;2&quot;&gt;
  
                              
                                                              &lt;input class=&quot;prodsize&quot; name=&quot;productattrsize&quot; type=&quot;text&quot; value=&quot;small&quot;&gt;
                                  &lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;
                                      &lt;div class=&quot;d-flex flex-row&quot;&gt;
                                                                                  &lt;!-- Product price--&gt;
                                                                                      &lt;del&gt;
                                                  &lt;p class=&quot;lead text-muted text-decoration-line-through&quot;&gt;sh 200&lt;/p&gt;
                                              &lt;/del&gt;
                                              &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; style=&quot;float-right&quot;&gt;sh 180&lt;/p&gt;
                                                                              &lt;/div&gt;
                                  &lt;/div&gt;
                      
                                                          
                              &lt;div class=&quot;d-flex &quot;&gt;
                                  &lt;a href=&quot;http://djvoskill/merchadise/bmerchadise-2-noattributes/2&quot; class=&quot;p-3 bg-dark text-white btn btn-primary ml-2&quot;&gt;View Product&lt;/a&gt;
                                  &lt;a class=&quot;p-3 bg-dark text-white btn btn-primary ml-2 add-to-cart3&quot;&gt;Add to Cart&lt;/a&gt;
                                  &lt;div id=&quot;successmsg3&quot; class=&quot;alert alert-message bg-grey text-dark&quot; style=&quot;display: none;&quot;&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                                  &lt;div class=&quot;col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1 mb-5&quot; style=&quot;border: 2px solid green&quot;&gt;
                  &lt;div class=&quot;card&quot;&gt; 
                      &lt;img class=&quot;card-img-top&quot; src=&quot;http://djvoskill/images/productimages/small/Merchadise 1 Attributes-6135.jpg&quot;&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
                          &lt;h6 class=&quot;font-weight-bold pt-1&quot;&gt;1.a.Merchadise 1 NoAttributes&lt;/h6&gt;
                          &lt;div class=&quot;d-flex align-content-center justify-content-center&quot;&gt;
                              &lt;span class=&quot;pt-1&quot;&gt;Tshirts&lt;/span&gt;
                          &lt;/div&gt;
                          1.&lt;span class=&quot;pt-1&quot;&gt;cotton&lt;/span&gt;
                          &lt;hr&gt;
                          2.&lt;span class=&quot;pt-1&quot;&gt;wedding&lt;/span&gt;
                          &lt;div class=&quot;fs-sm mb-4&quot;&gt;
                              &lt;input name=&quot;quantity&quot; type=&quot;number&quot; value=&quot;1&quot; class=&quot;prod_qty&quot;&gt;
                              &lt;input class=&quot;prod_id4&quot; name=&quot;product_id&quot; type=&quot;hidden&quot; value=&quot;1&quot;&gt;
  
                              
                                                              &lt;input class=&quot;prodsize&quot; name=&quot;productattrsize&quot; type=&quot;text&quot; value=&quot;small&quot;&gt;
                                  &lt;div class=&quot;d-flex align-items-center justify-content-between pt-3&quot;&gt;
                                      &lt;div class=&quot;d-flex flex-row&quot;&gt;
                                                                                  &lt;!-- Product price--&gt;
                                                                                      &lt;del&gt;
                                                  &lt;p class=&quot;lead text-muted text-decoration-line-through&quot;&gt;sh 1&lt;/p&gt;
                                              &lt;/del&gt;
                                              &lt;p class=&quot;lead text-muted text-decoration-line-through&quot; style=&quot;float-right&quot;&gt;sh 0.9&lt;/p&gt;
                                                                              &lt;/div&gt;
                                  &lt;/div&gt;
                      
                                                          
                              &lt;div class=&quot;d-flex &quot;&gt;
                                  &lt;a href=&quot;http://djvoskill/merchadise/amerchadise-1-noattributes/1&quot; class=&quot;p-3 bg-dark text-white btn btn-primary ml-2&quot;&gt;View Product&lt;/a&gt;
                                  &lt;a class=&quot;p-3 bg-dark text-white btn btn-primary ml-2 add-to-cart4&quot;&gt;Add to Cart&lt;/a&gt;
                                  &lt;div id=&quot;successmsg4&quot; class=&quot;alert alert-message bg-grey text-dark&quot; style=&quot;display: none;&quot;&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;              
  &lt;/div&gt;
  </code></pre>
  <p>how can i make it happen such that if the product selects an option the selected option is passed but if the product doesnt have attributes the value of small is passed to the function.</p>