www.webdeveloper.com
Results 1 to 11 of 11

Thread: change variable onclick

  1. #1
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210

    change variable onclick

    Hi,

    is it possible to change a variable on click, somthing like:

    Code:
    var a = ab;
    
    div.innerHTML = div.innerHTML+'<span onclick="var a = b">Change</span>';
    
    			if (a == b) {
    				alert ('you clicked change');
    			} else {
    				alert('no');
    			}
    Simon

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    is it a a Global variable or a local one?

  3. #3
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    local

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    and the function where that variable lays is?

  5. #5
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    The variables lies in one function with the div write like:

    Code:
    function addup(){
    
    var a = ab;
    
    div.innerHTML = div.innerHTML+'<span onclick="var a = b">Change</span>';
    
    if (a == b) {
    	alert ('you clicked change');
    		} else {
    	alert('no');
    		}
    
    }

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    first note: innerHTML is not a standard DOM method, thus it can not carry the information about the function where it was called.

    can you detail your aim and tell us why do you need that, in fact?

  7. #7
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    that was just from the code before I stripped it down, you can just replace it with document.write as its not needed.

    function addup(){

    var a = ab;

    document.write ('<span onclick="var a = b">Change</span>');

    if (a == b) {
    alert ('you clicked change');
    } else {
    alert('no');
    }

    }

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    back to your genuine need: do you need to know if a certain element was clicked? WhY?

  9. #9
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    this is stripped down from my original code, inside the if is not going to be an alert but an event

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    The way you have written that is not logical nor possible, because you defined a local variable before you try to change it's value. What's the use? A local variable lives only inside that function. Whenever you will call that function again, that variable will be defined again with it's "old" value.

    Your aim is confusing. This is the reason for I asked you to tell us what is your real need. I don't think you are interested in a variable called a, are you?

  11. #11
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    I dont have a need for a

    but I quickly typed this up to find out how to do it in my code. I would post my full code on here but it is very messy and confusing

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles