Ravi Ravi - 6 months ago 42
Javascript Question

Error in extract value from href with JQuery in Laravel 5

My blade is loading like this

<table class="table table-hover" style="table-layout: fixed;">
@foreach ($product as $product)
<tr>
<td><h6><img src="{{asset('assets/products/'.$product->img)}}" class="img-responsive" style="max-height: 100px"></h6></td>
<td>{{$product->product_name}}</td>
<td style="word-wrap:break-word; width: 45%;">{{$product->description}}</td>
<td>${{$product->product_price}}</td>
<td colspan="3">
<a href="{{url('admin/set/status/product?id='.$product['product_id'])}}" onclick="return confirm('Are you sure ?')" class="{{$product['status']==1 ? 'btn btn-danger btn-lnk' : 'btn btn-success btn-lnk'}}">{{$product['status']==0 ? 'Enable ':'Disable'}}</a>
<a href="{{url('/admin/product/edit?id='.$product['product_id'])}}" class="btn btn-info btn-lnk">Edit</a>

//modal load from this link
<a href="#{{$product['product_id']}}" class="btn btn-success btn-lnk" data-toggle="modal" data-target="#myModal" id="code">+Add File</a>
</td>
</tr>
@endforeach
</table>


My model is like this

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Upload Your File for {{$product['product_name']}}</h4>
</div>
<!-- START OF MODAL BODY-->
<div class="modal-body" style="padding-left: 10%">
<p>Please choose a file to upload. EXE, ISO, RAR, ZIP.</p>
<form role="form" method="post" class="form-horizontal" action="{{url('admin/addproductexe')}}" id="frm_01" enctype="multipart/form-data">

//load value from <a href>
<input type="text" name="Id" id="Id" value=""/>


<label for="file">File input</label>
<input id="uploadBtn" type="file" class="" name="fileField" />
<p></p>
<button type="submit" class="btn btn-info btn-lnk">Upload</button>
</form>
</div>
<!-- END OF APPLICATION FORM MODAL BODY -->
<div class="modal-footer">
<button type="button" class="btn btn-danger btn-lnk" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>


And my JS is this

<script>
var clearBn = $("#code");
clearBn.on("click", function(){
var link = $(this).attr('href');
var hashPosition = link.indexOf('#'); //Get the position of '#'
var number = link.substring(hashPosition + 1); //Split the string and get the number.
$(".modal-body #Id").val( number );
});
</script>


finally this kind of href link is generating

/public/admin/product/all#3


I'm getting only first link value only. What's the wrong with this :(

Highlight: I'm generating my view with a foreach. I think that is why other links doesn't act as I wish

Answer

This is expected behavior, As you are creating anchor element in a loop thus its creating element with same Id.

Identifiers in HTML must be unique, use a common class then Class Selector (“.class”) can be used. like

Script

 var clearBn = $(".code");

HTML, Here I have add class code to the element and removed Id attribute

<a href="#{{$product['product_id']}}" 
  class="btn btn-success btn-lnk code" 
  data-toggle="modal" 
  data-target="#myModal">+Add File</a>
 </td>