0

I can't figure out how to assign this function's result into a global variable. I know this is a really basic thing, but can anyone help?

var pixel_code = null


function captureValue(){
  pixel_code = document.getElementById("baseText").value;
 return pixel_code;
}

pixel_code = captureValue();
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • What you have should work (twice ;)). Is all this code maybe inside another function? – Joseph Silber Jan 20 '13 at 05:19
  • does the baseText element have a value attribute set ? – canolucas Jan 20 '13 at 05:24
  • 3
    AT @canolucas, why in the world did you edit the OP code so it was totally different? I rolled it back since you really changed it. – epascarello Jan 20 '13 at 05:28
  • This code is not inside another function. When I try to console.log(pixel_code) outside of the captureValue function I get undefined. Inside of the function it outputs as I intend it to. – Victor Torres Jan 20 '13 at 05:29
  • 1
    It works for me: http://jsfiddle.net/UrFyF/ – Joseph Silber Jan 20 '13 at 05:30
  • @epascarello the value is being set twice – canolucas Jan 20 '13 at 05:31
  • 4
    @canolucas - So post a comment or answer about that. Don't "correct" the OP's code directly in the question, or the question doesn't make sense. – nnnnnn Jan 20 '13 at 05:32
  • http://jsfiddle.net/AsNjd/3/ Here's the full spectrum of what I am trying to do – Victor Torres Jan 20 '13 at 05:36
  • @VictorTorres - Your fiddle does work, there's just no text in the `textarea`: http://jsfiddle.net/AsNjd/4/ – Joseph Silber Jan 20 '13 at 05:42
  • Victor, there are several issues with the fiddle, e.g., you shouldn't have elements within the ``, but more importantly you have your JS within an `onload` handler because that is what jsfiddle does by default if you don't change it in the panel on the left - you should change that to "no wrap (head)". Regarding what you're trying to achieve, you call the function from a button click handler, but don't have other code that tries to use that global variable. – nnnnnn Jan 20 '13 at 05:43
  • What do you mean by "global variable" ? http://www.snook.ca/archives/javascript/global_variable ? Or something else ? – Thong Kuah Jan 20 '13 at 05:44
  • 1
    I think my mistake was that the console.log(pixel_code) was running right when the page was loaded, but I was expecting it to log after I executed the function. – Victor Torres Jan 20 '13 at 06:05
  • Is there a way I can make the console.log wait till I run the previous function first before it logs? – Victor Torres Jan 20 '13 at 06:09

4 Answers4

0

You're reusing pixel_code in and out of the function, which is not a great pattern, but the code you show should work as expected. What error are you seeing? What code surrounds this code that you're not showing? Could all this perhaps be nested inside another function? (Thanks @JosephSilver for the nod.)

robrich
  • 13,017
  • 7
  • 36
  • 63
  • 1
    This is not an answer. You should post it as a comment. – Joseph Silber Jan 20 '13 at 05:21
  • if I run console.log(pixel_code) or document.write(pixel_code) outside of the function I only get undefined. Inside of the function it works fine. – Victor Torres Jan 20 '13 at 05:26
  • Previously you had `pixel_code = captureValue();` and the function had `return ...`. If that's the case, even with the reused var you should get the correct result. Perhaps you could create an example on jsfiddle.com – robrich Jan 20 '13 at 05:28
  • @robrich I posted the jsfiddle on the comment stream under the question – Victor Torres Jan 20 '13 at 05:41
  • Thanks for the jsfiddle. (I missed it the first go-round because I had to click "show more comments". Silly me.) See my new answer. – robrich Jan 20 '13 at 06:15
0

If the page reloads, your variable will be reset to it's initial state.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
0

Please try this,

var pixel_code='';

function captureValue(){
  return document.getElementById("baseText").value;
}

function getValueBack()
{
    pixel_code = captureValue();
    //alert(pixel_code); /* <----- uncomment to test -----<< */
}
halfer
  • 19,824
  • 17
  • 99
  • 186
NBaua
  • 583
  • 4
  • 16
  • 33
0

Thanks for sharing the jsfiddle of what you were attempting. I see the concern. The captureValue() function is run asynchronously, so the console.log() shortly after defining it doesn't yet have a value. I've stripped and prodded the jsfiddle and come up with this working sample:

<html>
    <head>
    </head>
    <body>
    <h1>Welcome to the AdRoll SandBox</h1>

    <textarea id="baseText" style="width:400px;height:200px"></textarea><br />

    <input type="button" value="test" id="text_box_button" onclick="captureValue()"/>
    <input type="button" value="get" id="text_box_button2" onclick="getValue()"/>
    <script>
var pixel_code = null;

function captureValue(){
    pixel_code = document.getElementById("baseText").value;
    return false;
}

function getValue() {
    alert(pixel_code);
    return false;
}
    </script>
    </body>
</html>

I added a second button. Type in the textbox, push "test" (to set the value), then push "get" to get the value of the global variable.

Here's the same sample that uses jQuery and a closure to avoid the global variable:

<html>
    <head>
    </head>
    <body>
    <h1>Welcome to the AdRoll SandBox</h1>

    <textarea id="baseText" style="width:400px;height:200px"></textarea><br />

    <input type="button" value="test" id="text_box_button" />
    <input type="button" value="get" id="text_box_button2" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
$(document).ready(function () {
    var pixel_code = null;

    $("#text_box_button").click(function (){
        pixel_code = document.getElementById("baseText").value;
        return false;
    });

    $("#text_box_button2").click(function () {
        alert(pixel_code);
        return false;
    });
});
    </script>
    </body>
</html>
robrich
  • 13,017
  • 7
  • 36
  • 63