1

how can I set a specific value on click? For example:

 $("input[type='button']").click(function() {
   switch(this.id) {
     case 'ClickButtonOne': //VARIABLE1=something; break;
     case 'ClickButtonTwo': //VARIABLE1=something; break;
   }
 });

Then print the value somehow onto the screen. Thank you! :)

3 Answers 3

2

perhaps is possible set id to number/value:

   <html><head>
<script type="text/javascript">

function whichElement(e)
{
var targ;
if (!e)
  {
  var e=window.event;
  }
if (e.target)
  {
  targ=e.target;
  }
else if (e.srcElement)
  {
  targ=e.srcElement;
  }
if (targ.nodeType==3) // defeat Safari bug
  {
  targ = targ.parentNode;
  }

myVariable=targ.id;

alert("My variable is "+myVariable);
}
</script>
</head>

<body id="-1" onmousedown="whichElement(event)">

<p id="3.14">click here to set variable to 3.14</p>

<h3 id="5">click here to set variable to 5</h3>
<p id ="2">click here to set variable to 2</p>


</body></html>

other possibility is obtain number(value) from text/name of element (do not use id).

Sign up to request clarification or add additional context in comments.

Comments

1
 $("input[type='button']").click(function() {
   var variable1 = 'default value'; // or add a default in the switch
   switch(this.id) {
     case 'ClickButtonOne':
         variable1 = 'something';
         break;
     case 'ClickButtonOne':
         variable1 = 'something else';
         break;
   }

   $('.the-element-you-want-to-add-it-to').html(variable1);
 });

Comments

0

make the variable global by defining it out of the scopes

var someVar = '';
$(document).ready(function(){
    $("input[type='button']").click(function() {
        switch(this.id) {
            case 'ClickButtonOne':
                someVar = 'something';
            break;
            case 'ClickButtonTwo':
                someVar='something else';
            break;
        }
        $("#someContainer").html(someVar);
    });
});

from what i understood (from the question), he wanted to set a variable which is already defined somewhere else. For this, it will have to be global. you can also store it in an HTML element using jquery (.data). Normally what i do is create a javascript object that will contain relevant data. something like :

var someDataContainer = {
    somethingImportant : 5
};

and later on i could refer to it using someDataContainer.somethingImportant

5 Comments

Why would you want to make that variable global? Please don't suggest this when it is not needed.
Hmm, what are the roles of global and local variables in jquery?
@pufAmuf: Same as in other languages :) Using global vars when it is needed only clutters stuff and is most of the time definiatly not needed nor wanted.
Thank you :). I would like to use those variables later when sending them off to a php file to request data from a mysql database. Can they stay local for that? @Peeha & Galchen - Thanks for your contributions I really appreciate your time :)))
@pufAMuf: Well you can just grab the contents of the element you just filled with the data: $('.the-element-you-want-to-add-it-to').html() will now contain the data you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.