Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript / JQuery .. Change variable values

Posted on 2011-02-25
4
Medium Priority
?
615 Views
Last Modified: 2012-05-11
Hi all,
I need some javascript code to change the value of a javascript variable when a link in clicked.

For example .. I have :

<script>

var one = '0';
...
</script>

<body>
<div id="myID">
<a href="whatever">click here</a>
</div>

So, when the link is click "IF" variable "one" is "0" then incrememnt to "1" otherwise don't do anything.

Any questions just ask.

Thanks again

0
Comment
Question by:error77
  • 2
4 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 34977822
$(document).ready(function(){
var one = '0';

  $("#myID").click(function(){
    one++;
      alert(one)
  });
});
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 1000 total points
ID: 34977831
Try this:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<script>

    var one = 0;

    $(document).ready(function(){
    
            $('a[href=whatever]').click(function(e){
                one = one + 1;
                $('#variable').html(one)
                e.preventDefault();
            });
    });

</script>


<body>
<div id="myID">
<a href="whatever">click here</a>
</div>

<div id="variable"></div>

</body>
</html>

Open in new window

0
 
LVL 20

Assisted Solution

by:chaitu chaitu
chaitu chaitu earned 1000 total points
ID: 34977832
$(document).ready(function(){
var one = '0';

  $("#myID").click(function(){
  if(one == '0')
    one++;

      alert(one)
       return false;
  });
 
});
0
 

Author Closing Comment

by:error77
ID: 34978045
Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question