Home > database >  how can i use laravel code in java script
how can i use laravel code in java script

Time:02-10

i want to translate button name in javascript by write laravel code

$(document).on('click', '.leave-edit-btn', function() {
        $(this).removeClass('leave-edit-btn').addClass('btn btn-white leave-cancel-btn').text("{{ __('trans.Edit') }}");

how can i do this?

(copied from comment): this my code when i click on button no translation run

@section('scripts')
<script> 
$(document).on('click', '.leave-edit-btn', function() { 
  $(this).removeClass('leave-edit-btn').addClass('btn btn-white leave-cancel-btn').text({{ __('trans.Save') }}); 
});
</script>
@endsection 

CodePudding user response:

based on the comments, are you sure your file is *.blade.php not just *.php? and it need to be under resources/view and use it as a view

are you sure your js is actually works? like the .leave-edit-btn is exists

you may try this btw

@section('scripts')
<script> 
$(document).ready(()=>{
  $(document).on('click', '.leave-edit-btn', function() { 
    $(this).removeClass('leave-edit-btn').addClass('btn btn-white leave-cancel-btn').text(@json(__('trans.Save'))); 
  });
});
</script>
@endsection 

CodePudding user response:

Using .text(text) to set an elements text requires the parameter to be a string. Use quotes around {{ __('trans.Save') }} like so:

@section('scripts')
<script> 
$(document).on('click', '.leave-edit-btn', function() { 
  $(this).removeClass('leave-edit-btn').addClass('btn btn-white leave-cancel-btn').text("{{ __('trans.Save') }}"); 
});
</script>
@endsection 
  • Related