Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
How to force disable the modal or a button?

When my users are readOnly, I want to limit some access. In this case, the ability to edit notes. I can't explain why with this code, I still can open up my note when the user is readOnly

if(codeParam == '{{ $baby->readOnlyCode }}' ) {

    //I am in side this code 
    $("a.btn, .btn-link, #logNote, pre, .btn").click(function() {
        return false; 
    });
}

https://mybabies.app/baby/797b808a-e8c6-4a83-acf4-14ebe1f776b1?code=rithys4k

Please click on the note box, you will see that somehow it still be able to trigger.

enter image description here

I even tried added the check and return false in showModal()

function showModal(val, type, logId ) {

    console.log("val, type, logId",val, type, logId);


    if(codeParam == '{{ $baby->readOnlyCode }}' ) {
       return false; 
    }

    ...

or tried preventDefault

if(codeParam == '{{ $baby->readOnlyCode }}' ) {
    $("a.btn, .btn-link, #logNote, pre, .btn").click(function(e) {
        e.preventDefault(); 
        return false; 
    });
}

If you spotted what I missed, please let me know.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The problem you're having is you're attaching a second event listener that only returns false and prevents default, but the first event listener that pops the modal is still attached.

I.e.,

$("a.btn").click(function(e) {
    alert("Hello World!");

});

$("a.btn").click(function(e) {
    return false; // this does nothing to the previously attached event listener

});

Possible solutions:

From inspecting your code, you have some options:

$(document).on('show.bs.modal', '#noteModal', function (e) {
    // move the check for the param here
    if(codeParam == 'rithys4k' ) {
        return false; 
    }
});

Or: Use disabled attributes on the buttons/links when in readonly mode.

To give you some more background - $().click() is really just shorthand for addEventListener on the click event

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to use on method with suffix in jQuery, on jQuery Docs

const clickAction = (e) => {
    // do anything
}

$('.btn, pre').on('click.go', clickAction);

Use off to remove events, off jQuery Docs

$('body').off("click.go", ".btn, pre");

// OR

$('body').off("click.go", ".btn, pre", clickAction);

// OR

$('.btn, pre').off("click.go");
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can debug the event order like this: $._data($("#logNote").get(0), "events");. New events go to the end and executed from top to bottom. In your case all you need to do is remember the trigger function and remove/add it when needed, example:

  const cbToggleNoteModal = () => $("#toggleNoteModal").click();
  // you have to store the cb in a variable

  $("#logNote").click(cbToggleNoteModal);

  // to disable the cb, just de-register the callback
  $("#logNote").off("click", cbToggleNoteModal);

  // and register it back when you need it
  $("#logNote").click(cbToggleNoteModal);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda