Home > database >  how to Concatenate an id in a foreach loop using jquery laravel
how to Concatenate an id in a foreach loop using jquery laravel

Time:08-10

i have a foreach loop whereby i want to concatenate the id of each loop to its id jquery.i have tried this but i get the the id for the first loop only.this is my code in the blade file.

 @foreach ($rentalcategories as $category)
  <div >
    <input type="text" value="{{ $category->id }}" >
    <input type="radio" name="rentalcategory"  id="rentalcat{{ $category->id }}" value="{{ $category->id }}">
        <label for="rentalcat" > {{ $category->rentalcat_title }}</label><span>({{ $category->rentalhses->count() }})</span>
  </div>
@endforeach

this is my jquery code

var categoryid=$(".catid").val();
console.log(categoryid);
var hsecategory= $('#rentalcat'   categoryid).prop('checked') ? $('#rentalcat'   categoryid).value : ''

on the console logging the categoryid variable am only getting the first value only for the first loop but the other one it doesn't show.how can i get the id for each loop then concatenate to an id in each loop. here in my code i want to get the value for each loop using the id instead of class.how can i achieve this

CodePudding user response:

I think because the Id as incrementing within the loop you just need to find element by id that starts with rentalcat

so something like this

$('input[id^="rentalcat"]')

credit to : Find html element which id starts with

that should give all the instances so you will need to preform an each function to get out each instance value

$('input[id^="rentalcat"]').each(function( i ) {
   console.log($(this).val())
});

you can then concatenate that values or do whatever you need with the id's in the each loop

I hope this helps

  • Related