La variable no está definida

I am trying this code to update the fallback of tipsy plugin. How can I access the variable a outside of first function ? I can override the variable to make the update, correct ?

<script type="text/javascript">
$(document).ready(function () {
    var a ="Login";
    $("#login_form").submit(function () {
        var formdata = $("#login_form").serializeArray();
        $.ajax({
            url: "ajax_login.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function (data) {
                if (data.livre === 'complete') {
                    var a ="success";
                } else 
                    var a = "Error";
            }
        });
return false;
    });
});
</script>

<script type='text/javascript'>
$(document).ready(function () {
    $('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a }); // a is not defined
});
</script>

preguntado el 08 de noviembre de 11 a las 19:11

5 Respuestas

Either move the code within the second document.ready block to the first one (which will make the a variable accessible by your .tipsy() call) or make the a variable a global.

<script type="text/javascript">
$(document).ready(function () {
    var a ="Login";
    $("#login_form").submit(function () {
        var formdata = $("#login_form").serializeArray();
        $.ajax({
            url: "ajax_login.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function (data) {
                if (data.livre === 'complete') {
                    a ="success";
                } else 
                    a = "Error";
            }
        });
return false;
    });
    $('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a });

});
</script>

OR

<script type="text/javascript">
var a = "Login";
$(document).ready(function () {
    $("#login_form").submit(function () {
        var formdata = $("#login_form").serializeArray();
        $.ajax({
            url: "ajax_login.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function (data) {
                if (data.livre === 'complete') {
                    a ="success";
                } else 
                    a = "Error";
            }
        });
return false;
    });
});
</script>

<script type='text/javascript'>
$(document).ready(function () {
    $('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a }); // a is not defined
});
</script>

Observe que eliminé el var declaration from the success callback on your AJAX call.

ACTUALIZACIÓN

To pass the updated value of a to tipsy you will need to run the .tipsy() call in your callback function. You could also run it as you are now and update the tipsy plugin in your callback function (however I am not familiar with the plugin and am not aware of how to do this):

<script type="text/javascript">
$(document).ready(function () {
    var a ="Login";
    $("#login_form").submit(function () {
        var formdata = $("#login_form").serializeArray();
        $.ajax({
            url: "ajax_login.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function (data) {
                if (data.livre === 'complete') {
                    a ="success";
                } else {
                    a = "Error";
                }
                $('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a });
            }
        });
return false;
    });
});
</script>

respondido 09 nov., 11:00

solves the error, but the tooltip is not updated. Is always "Login", never changes to success or error. - Daniel

The AJAX call is asynchronous, meaning that it executes the callback function after the .tipsy() call has been made. Check-out my change above to load tipsy in your callback function. Another solution would be to load tispy as you are but update it in the callback function (I don't know the plugin personally so I'm not sure how to do this). - Jaspe

It is not possible to change the fallback property of your Tipsy objects after they've been created without hacking the tipsy plugin misma.

Dado que fallback parameter is a string, it is interpreted as soon as the $().tipsy({...}) function is executed. So changing the value of variable a afterwards will not change the fallback parámetro.

I first thought that directly updating the fallback propiedad de la $.fn.tipsy.defaults object would make it, but when a new Tipsy object is created, the fallback property is basically copied in it, so it will store the initial value of fallback por sienpre.

One solution would be to fork the Tipsy project and change the fallback property to accept either a string or a function(). Like this it would be possible to do something like: fallback: function (){ return a;}.

respondido 09 nov., 11:03

i will make a simple css tooltip with dynamic content and a fade. Keep it simple, and solved. thanks - Daniel

Mueva el a variable outside the ready function:

<script type="text/javascript">
    var a ="Login";
    $(document).ready(function () {

Además, no use el var keyword in your success function. You are re-declaring the variable, rather than setting the value of the variable defined at the beginning of the ready function.

 success: function (data) {
            if (data.livre === 'complete') {
                a ="sucess";
            } else 
                a = "Error";
        }

respondido 08 nov., 11:23

You could make it global. Whether or not that's what you debemos do depends.

If it's all running in doc ready, there's no reason to have a global; you could just run the code from the second docready from the ajax success (or complete, if it meets your needs).

Hard to say; don't know how close the snippets are to reality.

respondido 08 nov., 11:23

1) why do you have 2 Document.ready ??

2)you can use global param or $.data to transffer data.

3)for your solution - declare the var a ="Login"; out side or use self Executing func which keeps the scope for the global var.

respondido 08 nov., 11:23

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.