-1

Im using this element

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" state="login">

and the script

$("#toggleLogin").click(function() {

    if ($("#exampleModal").data("state") === "login") { alert("it works"); } else { alert ("not working");};



<script `src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" crossorigin="anonymous"></script>`

I look forward having an script, that if the value of "state" is 'login' it will be switched to 'signup' and else will be 'login' back again

I've found this example here but it's not working for me

Check if data attribute value equals a string

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
Tinchoman
  • 35
  • 1
  • 5
  • why do you think this is php related? You tagged it as such. – Funk Forty Niner Aug 20 '18 at 02:04
  • It looks strange that you want to toggle a data attribute's value of a bootstrap modal... What will it be used for if you don't event know how to change the value? Is your next question about how to change the modal's conent based on it? What do you really want to do here? – Louys Patrice Bessette Aug 20 '18 at 02:56
  • @LouysPatriceBessette yes, I want to have the signup form hidden (just few extra fields) and have them displayed when the data-state value is signup – Tinchoman Aug 20 '18 at 04:31

1 Answers1

1

Reordering your scripts to include jQuery in advance. Then change state="login" to data-state="login" in #exampleModal tag.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" crossorigin="anonymous"></script>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-state="login"></div>

<button id="toggleLogin">Toogle Login</button>


<script>
$("#toggleLogin").click(function() {

    if ($("#exampleModal").data("state") === "login") {
        $("#exampleModal").data("state", "signup");
    } else {
        $("#exampleModal").data("state", "login");
    }
    console.log($("#exampleModal").data("state"));
    
});
</script>
Chaska
  • 3,165
  • 1
  • 11
  • 17