Home > Blockchain >  Stop opening of Bootstrap Accordion when textbox is clicked using a listener function
Stop opening of Bootstrap Accordion when textbox is clicked using a listener function

Time:10-18

I have a Bootstrap Accordion I need to prevent opening of the accordion when textbox is clicked While, if anyone clicks outside the texbox (blue color region) let it expand and shrink as usual.

So I have tried with

            $(".accordion-button").click(function(event) {
        //console.log($('.workflowTitle:focus').length)

        if($('.workflowTitle:focus').length >= 1)
        {
          event.stopPropagation();
          event.preventDefault();
          return
        }
            });
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div  id="accordionExample">
  <div >
    <h2  id="headingOne">
      <button  type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <input  value="Accordion Item #1"/>
      </button>
    </h2>
    <div id="collapseOne"  aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div >
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>

CodePudding user response:

You can disable the according collapse when you input gets focus and re-enable it on it's blur event.

$('.accordion .accordion-header input').on('focus', function(event) {
  $(this).parent().attr('data-bs-toggle', 'disabled');
}).on("blur", function() {
  $(this).parent().attr('data-bs-toggle', 'collapse');
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div  id="accordionExample">
  <div >
    <h2  id="headingOne">
      <button  type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <input  value="Accordion Item #1"/>
      </button>
    </h2>
    <div id="collapseOne"  aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div >
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
        hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>

CodePudding user response:

I looked through Bootstrap docs but I couldn't find a built-in way to implement such functionality so I will showcase a possible solution that acts on the events that Bootstrap's collapsible components emit.

The idea is simple, we'll have a global variable that acts as a flag (true or false) and based on that flag we either allow the open/close mechanisms of the accordion or simply prevent them.

Here's what we're going to do:

  • initialize a global variable, let's call it shouldPreventAccordion, that acts as a flag to tell whether opening/closing of the accordion should be prevented.
  • listen for the focus event on the input.workflowTitle and set the flag, shouldPreventAccordion, to true.
  • listen for the blur event on the input.workflowTitle and set the flag, shouldPreventAccordion, to false.
  • and finally, we listen for Bootstrap's collapsible components events, mainly show.bs.collapse and hide.bs.collapse and based on our flag we either prevent the action or allow it.

Here's a live demo to illustrate:

/** our flag */
let shouldPreventAccordion = !1;

/** when the input is focused we should prevent the accordion from opening/closing */
$(".workflowTitle").on('focus', () => shouldPreventAccordion = !0);

/** when the input is blured (lost focus) we should allow the accordion from opening/closing */
$(".workflowTitle").on('blur', () => shouldPreventAccordion = !1);

/** when the accordion is about to open we should check if that action is allowed using our flag */
$(".accordion").on('show.bs.collapse', e => {
  shouldPreventAccordion && e.preventDefault();
  return !shouldPreventAccordion;
});

/** when the accordion is about to close we should check if that action is allowed using our flag */
$(".accordion").on('hide.bs.collapse', e => {
  shouldPreventAccordion && e.preventDefault();
  return !shouldPreventAccordion;
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div  id="accordionExample">
  <div >
    <h2  id="headingOne">
      <button  type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <input  value="Accordion Item #1"/>
      </button>
    </h2>
    <div id="collapseOne"  aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div >
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
        hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>

The above solution seems natural is we're listening to event emitted by Bootstrap and we're not changing any DOM attributes should be changed manually such as the attribute used by Bootstrap to control it functionalities.

  • Related