Solved

Javascript / JQuery .. Change variable values

Posted on 2011-02-25
4
606 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
Comment Utility
$(document).ready(function(){
var one = '0';

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

Accepted Solution

by:
Lukasz Chmielewski earned 250 total points
Comment Utility
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
Comment Utility
$(document).ready(function(){
var one = '0';

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

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

Author Closing Comment

by:error77
Comment Utility
Thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now