Solved

Javascript / JQuery .. Change variable values

Posted on 2011-02-25
4
608 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 250 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 250 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…
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…

773 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