Solved

Javascript / JQuery .. Change variable values

Posted on 2011-02-25
4
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

751 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