JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 25th, 2011, 12:37 PM
idlackage@yahoo idlackage@yahoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 34 idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 58 m 56 sec
Reputation Power: 8
Cannot assign variable to element

I have several lines of code in different functions where I need to refer to elements in document.form.max, so I figured I would define it as a global variable called tb1 to shorten the writing. The result however is the page senses an error as soon as it gets to tb1.value, of which I can't figure out why since things work when I change tb1 into a local variable. I was under the impression that global = full scope, so I'm not sure what's wrong here.

Code:
<html>
<head>
<script language="javascript" type="text/javascript">

var tb1 = document.getElementById('textbox1');
	
function output() {
    var max = tb1.value;
    document.write( max );
}

</script>
</head>

<body>

<form>
<input name="max" type="text" id="textbox1"><br>
<input name="submit" type="button" value="Enter" onClick="output()">
</form>

</body>
</html>

Reply With Quote
  #2  
Old February 25th, 2011, 01:28 PM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,874 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 17 h 30 m 25 sec
Reputation Power: 2569
You are assigning the element 'textbox1' to your 'tb1' variable before the page has loaded, i.e. 'textbox1' doesn't exist yet. Put the variable within your function.
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Reply With Quote
  #3  
Old February 25th, 2011, 01:32 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Quote:
I was under the impression that global = full scope

1) Yes. However, DOM look-ups only work after the element has been created. You're trying to access an element before that part of the page has been loaded.

2) The language attribute of the <script> element is deprecated, so don't use it. Just use the type attribute.

3) document.write() will overwrite the contents of the page if it's used after the page has finished loading.
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #4  
Old February 25th, 2011, 02:19 PM
TheOtherDino's Avatar
TheOtherDino TheOtherDino is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Location: Katy, Texas
Posts: 489 TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 18 h 25 m 29 sec
Reputation Power: 198
What I'll typically do is define the global, but set it to null.

Then, which ever function needs it, can first test for null, and if null, calls a small function to set it.
__________________
Do you agree? Disagree? And remember, it's all about the reputation power...

Reply With Quote
  #5  
Old February 25th, 2011, 08:04 PM
cueball cueball is offline
Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 11 cueball User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 10 sec
Reputation Power: 0
Like the others said, you cannot access an element with javascript until it has been loaded.

You have all your js in the <head>, so none of the body elements have been created when your js is run.

You have 3 choices.

1) put the tb1 definition inside the function.

2) put the tb1 definition inside a window.onload event handler (see demo below) which runs after all the body elements and images have been loaded.

3) take your <script> element out of the <head> and put it just before the </body>.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            function output() {
                var max = tb1.value;
                document.getElementById('result').innerHTML = max;
            }
            window.onload=function(){
                tb1 = document.getElementById('textbox1'); //tb1 is now global
            }
        </script>
    </head>
    <body>
        <form>
            <input name="max" type="text" id="textbox1" /><br />
            <input name="submit" type="button" value="Enter" onclick="output()" />
        </form>
        <div id="result"></div>
    </body>
</html>

Reply With Quote
  #6  
Old February 26th, 2011, 07:50 PM
idlackage@yahoo idlackage@yahoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 34 idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 58 m 56 sec
Reputation Power: 8
Thanks to everyone, got the script working. When I transferred the idea of this to another script though, it didn't work for some reason.

I have this

Code:
function output() {
	a = document.getElementById( 'textbox1' );
	b = eval( a.value );
	// If box 'a' is empty, then 'b' will have a value of 10.
	isEmpty( a, b, 10 );
}

// If 'tbox' is empty, then variable 'n' will have a value of 'val'.
function isEmpty( tbox, n, val ) {
    if( tbox.value.length == 0 ){
        n = val;
    }
}


This worked when I directly referred to 'a' in isEmpty (replacing variable 'tbox' with just 'a'), but not when it's dynamic like right now--the value of 'b' would show up as undefined. What's the problem here?

Reply With Quote
  #7  
Old February 26th, 2011, 09:47 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
It's best to avoid using eval(). What purpose are you trying to achieve by using it here?

Reply With Quote
  #8  
Old February 26th, 2011, 11:05 PM
idlackage@yahoo idlackage@yahoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 34 idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 58 m 56 sec
Reputation Power: 8
Quote:
Originally Posted by Kravvitz
It's best to avoid using eval(). What purpose are you trying to achieve by using it here?


I'm trying to use it as a numerical value in an addition equation.

Reply With Quote
  #9  
Old February 26th, 2011, 11:22 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Then use one of these:
Code:
b = parseInt( a.value, 10 );
Code:
b = parseFloat( a.value );
Code:
b = Number( a.value );

Reply With Quote
  #10  
Old February 27th, 2011, 11:58 AM
idlackage@yahoo idlackage@yahoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 34 idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 58 m 56 sec
Reputation Power: 8
Quote:
Originally Posted by Kravvitz
Then use one of these:
Code:
b = parseInt( a.value, 10 );
Code:
b = parseFloat( a.value );
Code:
b = Number( a.value );


Thanks. Whether or not I'm using numerical though, things are still returning 'undefined'.

Reply With Quote
  #11  
Old February 27th, 2011, 01:47 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Both of those functions would return "undefined" because there is no return value specified.

What is the purpose of the isEmpty function anyway?

By the way, it's best to avoid using global variables.

Reply With Quote
  #12  
Old February 27th, 2011, 02:06 PM
idlackage@yahoo idlackage@yahoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 34 idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level)idlackage@yahoo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 58 m 56 sec
Reputation Power: 8
Quote:
Originally Posted by Kravvitz
Both of those functions would return "undefined" because there is no return value specified.


Am I supposed to return 'val'? Things have worked with no return value--isEmpty is only used to substitute values for shortening code, not really to do anything with the actual variables.

Quote:
What is the purpose of the isEmpty function anyway?


Suppose I have this in the output() function:

Code:
if( document.form.a_box.value.length == 0 ){
    a_val = 10;
}
if( document.form.b_box.value.length == 0 ){
    b_val = 1;
}
if( document.form.c_box.value.length == 0 ){
    c_val = 1;
}
if( document.form.d_box.value.length == 0 ){
    d_val = " | ";
}
if( document.form.e_box.value.length == 0 ){
    e_val = "\n\n";
}


The purpose of isEmpty is to do the exact same thing, except instead of rewriting ten if statements I can just write isEmpty( a_box, a_val, 10 ); instead.

Quote:
By the way, it's best to avoid using global variables.


I'm all for learning another way to do this, but for the purposes of just writing one small program, I'm not really concerned right now of what I should avoid as long as it works.

Reply With Quote
  #13  
Old February 27th, 2011, 02:46 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Using global variables with a helper function like that is more complicated than using local variables.

You could check if the value is an empty string instead of checking the length.

Perhaps this is what you want:
Code:
function output() {
  var a = document.getElementById( 'textbox1' );
  a.value = defaultValues( a.value , 10 );
}

function defaultValues( n, val ) {
  if( n == '' ){
    return val;
  }
  return n;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Cannot assign variable to element

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap