SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Hibernator YuriKolovsky's Avatar
    Join Date
    Nov 2007
    Location
    Malaga, Spain
    Posts
    1,072
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    help? changing a variable within a function with a function

    hello, im just starting out in JavaScript so excuse me for being very newbish

    i have this code here
    and i want to use it to change a variable withing function2() using MyFunction().

    Is This even possible!??!?!
    Code JavaScript:
    <html>
    <head>
    <title>Experimental Page</title>
    <script type="text/javascript">
    var $val;
    var $exp;
    $exp = "something";
    function MyFunction ($var) {
    	  $exp = "something2"
    }
    function function2() {
          document.write($exp);
          }
    </script>
    </head>
    <body>
     
    <div id="form6"></div>
    <form name="form1" method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
       <textarea cols="40" type="text" name="text7" id="textarea1"><? echo stripslashes($_POST['text7']); ?></textarea>type here
       <input type="submit" value="OK" />
    </form>
     
     
    <a onClick="MyFunction('1');">toggle</a>
    <br />
     
     
          <script type="text/javascript">
          function2();
          </script>
     
    </body>
    </html>

    any help is greatly appretiated

  2. #2
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your code above, $exp is a global variable (declared outside of any function) and you can access it from any function.

  3. #3
    Hibernator YuriKolovsky's Avatar
    Join Date
    Nov 2007
    Location
    Malaga, Spain
    Posts
    1,072
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    but why doesn't the other function change the value of $exp?

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by YuriKolovsky View Post
    but why doesn't the other function change the value of $exp?
    It does. But after function2 has executed.

    To achieve what you want, you shouldn't use document.write:

    Code JavaScript:
    <html>
    <head>
    <title>Experimental Page</title>
    <script type="text/javascript">
    var exp = "something";
    function MyFunction () {
      exp = "something2";
    }
    function function2() {
      document.getElementById("foo").innerHTML = exp;
    }
    </script>
    </head>
    <body>
     
    <div id="form6"></div>
    <form name="form1" method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
       <textarea cols="40" type="text" name="text7" id="textarea1"><? echo stripslashes($_POST['text7']); ?></textarea>type here
       <input type="submit" value="OK" />
    </form>
     
     
    <a onClick="MyFunction();">MyFunction</a>
    <a onClick="function2();">function2</a>
    <br />
     
     
    <div id="foo"></div>
     
    </body>
    </html>

  5. #5
    Hibernator YuriKolovsky's Avatar
    Join Date
    Nov 2007
    Location
    Malaga, Spain
    Posts
    1,072
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    ok, this works partially now
    the reason is why i tried to use document.write is because i wanted one function to change the variable of another function.
    now i'm starting to think that its supposed to be done differently
    the problem is that i have a huge list of functions that change the <textarea> tag making it "prettier" to type in, i want to toggle this, to activate those functions and deactivate them. while searching for this i stubled upon the idea that if i change a variable in one of the functions (the name of the id="" of textarea) then i can move the <textarea> decoration from one textarea to another.

    i think im sounding confusing, so ill try to explain in short.
    i have function(1) that changes textarea upon textarea id
    i want a button (function) to change this id withing this function(1)
    so that function(1) affects a different textarea, with a different id

    any ideas? or am i totally doing this the wrong way?

    P.S. sorry for late reply, was on my birthday vacation

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you really need the id of the target textarea to be variable? You can either pass the id as a parameter to the function, or you can use a variable which is accessible from both the function and the context that makes the toggling (Usually a global variable). I'm still not entirely sure what it is you're trying to accomplish, but perhaps this helps:
    HTML Code:
    <html>
    <script>
    function doStuffWithTextarea(areaId) {
      document.getElementById(areaId).style.border = "1px solid red";
    };
    </script>
    <body>
    <textarea id="ta1"></textarea>
    <textarea id="ta2"></textarea>
    <textarea id="ta3"></textarea>
    <input type="button" onclick="doStuffWithTextarea('ta1')" value="ta1" />
    <input type="button" onclick="doStuffWithTextarea('ta2')" value="ta2" />
    <input type="button" onclick="doStuffWithTextarea('ta3')" value="ta3" />
    </body>
    </html>

  7. #7
    Hibernator YuriKolovsky's Avatar
    Join Date
    Nov 2007
    Location
    Malaga, Spain
    Posts
    1,072
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    that would work, but the problem is that i dont need to style the textarea, i need to strip the textarea with id of 1 (for example) of all the functions that affect it (on body onLoad)

    so sorry if im confusing, in php this type of stuff usually makes sence, but in javascript everything seems to be overly complicated

    by the way, is there a way to deactivate a function? as in return it to its original deactivated form?

    i have this function, which i kinda like on how it works
    but i want to have a button to move and/or deactivate it

    bkLib.onDoLoaded(function() {

    new textEdit().controlInstance('textarea1');
    }

    or a javascript button that deactivates all javascript completely?

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by YuriKolovsky View Post
    that would work, but the problem is that i dont need to style the textarea, i need to strip the textarea with id of 1 (for example) of all the functions that affect it (on body onLoad)
    You can't do that. Functions execute and then they end. They may cause side effects while running (Such as changing the style of an element, or something else). To reverse this change, you need to write a function, which does the opposite. So for example, if you had a function like the above, that changes the border of an element, you could create a function that removes the border.

    Perhaps you could tell a little more about what you're trying to accomplish?

  9. #9
    Hibernator YuriKolovsky's Avatar
    Join Date
    Nov 2007
    Location
    Malaga, Spain
    Posts
    1,072
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You can't do that. Functions execute and then they end. They may cause side effects while running (Such as changing the style of an element, or something else). To reverse this change, you need to write a function, which does the opposite. So for example, if you had a function like the above, that changes the border of an element, you could create a function that removes the border.
    aha! interesting...

    makes sence.

    i was simply trying to deactivate the function, but that seems to be impossible.
    i actually did figure it out, i found a function that toggles this function, and added it to my mess (code).
    it works fine now

    as a clarification ill try to explain what i was trying to do, (you might consider it laughably simple).

    i tried to add a button that adds buttons like bold, italic to a textarea, which looks like a simple rich text editor, and a button to toggle it on and off.

    at least each time you mess around with things you learn lots of new things.

    thanks for the help

    p.s. would you mind me pm'ing you for any future javascript/php related problems?


Bookmarks

Posting Permissions

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