1

I am trying to change variable value on click, so that i can send the value via ajax to load data in ascending or descending format.

Here is Javascript part on top of page.

var ascending = '0';

Here is HTML Part.

<span onclick="knowFormat(true)"><input type="checkbox" id="ascending" name="ascending"><label for="ascending"><i class="fa fa-sort-alpha-asc"></i>&nbsp;Ascending</label></span>

<span onclick="knowFormat(false)"><input type="checkbox" id="descending" name="ascending"><label for="descending"><i class="fa fa-sort-alpha-desc"></i>&nbsp;Descending</label></span>

<span><input type="button" name="sort" onclick="sorting()"></span>

Here is my CSS :

input { display:none; }
input:checked ~ label { color:red; }

Here is my Javascript just before ajax function and after html, css part:

function knowFormat(ascending) {
    if (ascending) { ascending = '0'; } else { ascending = '1'; }
}

And in my Ajax, Data is always getting sent '0', even after click.

function sorting() {
alert(ascending);
    jQuery.ajax({
        url : 'ajax/file.php',
        type : 'POST',
        data : 'ascending='+ascending,
        success : function(s) {
            alert(s);
        }
    });
}

Even after clicking sorting, The alert button is always 0.

1 Answer 1

2

You are changing the parameter due to scope. The same parameter name hides the global variable. To change global variable change the name of parameter or global variable.

function knowFormat(ascending1) {
    if (ascending1) { ascending = '0'; } else { ascending = '1'; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

even after clicking , value of ascending is not getting changed.
Did you debug the code to check what you get in parameter ascending1?

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.