0

When a user clicks on "receiver list" button, the button text should change to "loading...". The buttons are in a For loop. I guess the id attribute method is not going to work. The logic is when a user clicks on "receiver list" button, the button will change to "loading..." and load the page it goes to.

@foreach($groups as $group)
  <div class="col-lg-4 col-md-6 col-sm-6">
    <div class="card card-stats">
      <div class="card-header card-header-success card-header-icon">
         <div class="card-icon">
            <i class="material-icons">store</i>
         </div>
         <a class="btn btn-info p-2" href="{{action('ReceiverController@index', $group)}}">
           <i class="fa fa-list-ol" style="height:55px; width:30px;" aria-hidden="true"></i>
               Receiver List
         </a>
      </div>                                    
    </div>
  </div>
3
  • Good, show us the code you've tried please Commented Sep 5, 2018 at 14:59
  • Umm...I tried various ways a couple weeks ago, and deleted all of them...I can write some code again, give me some time. Thx. Commented Sep 5, 2018 at 15:02
  • Take your time.. Commented Sep 5, 2018 at 15:02

2 Answers 2

1

just use inline onclick event of javascript:

<a class="btn btn-info p-2" onclick="this.innerHTML='loading...'" href="{{ action('ReceiverController@index', $group) }}">
    <i class="fa fa-list-ol" style="height:55px; width:30px;" aria-hidden="true"></i>
    Receiver List
</a>

if you want button icon as well:

<a class="btn btn-info p-2" onclick="clickfunc(this)" href="{{ action('ReceiverController@index', $group) }}">
    <i class="fa fa-list-ol" style="height:55px; width:30px;" aria-hidden="true"></i>
    Receiver List
</a>

<script>
clickfunc = function(obj) {
    obj.innerHTML = '<i class="fa fa-list-ol" style="height:55px; width:30px;" aria-hidden="true"></i> loading...';
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming the p-2 class is unique to these buttons, this should work simple enough:

$(document).on('click', '.p-2', function(){
    let html = `<i class="fa fa-list-ol" style="height:55px; width:30px;" aria-hidden="true"></i>
           Loading...`
    $(this).html(html);
});

If p-2 is not unique, then just add some other unique class name.

Or without jquery:

function load(){
     let html = `<i class="fa fa-list-ol" style="height:55px; width:30px;" aria-hidden="true"></i>
           Loading...`
    this.innerHTML = html;
}
document.getElementsByClassName('.p-2').addEventListener("click", load);

3 Comments

jquery has neither been mentioned nor tagged, please do not answer with libraries that are not requested - javascript does not automatically mean jquery
Great! You are awesome! What is this method called? JavaScript is a headache for me as always.
These are just two ways (1 jquery and 1 normal JavaScript) to add an event listener to some html DOM element. There are a lot more events than just click. Read more about them from w3schools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.