Solved

Javascript / JQuery .. Change variable values

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

617 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