0

I am trying to use the value of a variable I define using JavaScript for a JQuery function. All of this happens in one .html file. In the body, I create this script:

<script type="text/javascript">
var clicks = 1;
function onClick() {
    //Code here to change value of 'clicks'
};
</script>

Later in that file, I use JQuery to pull data from a server when a button called 'previous' is clicked. I use this code:

$('#previous').click(function(){
    $('#question').load('/php/getQuestion.php',
      { name: $("clicks").val()} ); //This line is wrong
}

How can I insert the value of 'clicks' into the 'name' field? Thanks

2
  • Need HTML, because it isn't obvious what "clicks" actually is supposed to be. 1. If a selector is wrapped in quotes $("tag") it's a <tag>, 2. if a selector is wrapped in quotes and is prtefixed with a period: $(".class") 3. If a selector is wrapped in quotes and is prefixed with a hashmark: $('#id'). $("clicks") means: *find any <clicks> elements. Of course that element doesn't even exist has the root of your problem. Commented Jul 9, 2017 at 22:14
  • If both of those blocks are within scope, then you have the real number 1 as a jQuery object which is invalid I think you'll get a type error... Commented Jul 9, 2017 at 22:20

2 Answers 2

2

clicks is a global variable, so you can use it directly:

$('#previous').click(function(){
    $('#question').load('/php/getQuestion.php',
      { name: clicks} );
}
Sign up to request clarification or add additional context in comments.

Comments

2

$("clicks") is looking for a form control element <clicks></clicks> that clearly doesn't exist and certainly wouldn't have a value.

You simply need to use the variable:

 $('#question').load('/php/getQuestion.php', { name: clicks} ); 

Comments

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.