Ravi Ravi - 2 years ago 107
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)
<td><h6><img src="{{asset('assets/products/'.$product->img)}}" class="img-responsive" style="max-height: 100px"></h6></td>
<td style="word-wrap:break-word; width: 45%;">{{$product->description}}</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>

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 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" />
<button type="submit" class="btn btn-info btn-lnk">Upload</button>
<div class="modal-footer">
<button type="button" class="btn btn-danger btn-lnk" data-dismiss="modal">Close</button>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

And my JS is this

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 );

finally this kind of href link is generating


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 Source

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


 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-target="#myModal">+Add File</a>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download