/    Sign up×
Community /Pin to ProfileBookmark

GET javascript variable from HTML without putting that variable in it’s own function?

So basically I’m trying to do:

[CODE]
JAVASCRIPT :
<script type =”text/javascript”>
function nightsmultiplicetxt() {
var NightPricev = 0000
var WeekPricev = 0000
var MonthPricev = 0000
}
</script>

HTML (wrong, but you see what I’m trying to do):
<input name=”calculate3″ type=”button” id=”calculate3″ tabindex=”2″
onclick= document._xclick.nightprice.value= ([U]nightsmultiplicetxt().NightPricev * 50[/U])[/CODE]

…help

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@XTREEMMAKauthorJun 11.2008 — Found the answer:

Variable scope is incorrect and cant be accessed that way. To correct, simply move the variables outside the function
Copy linkTweet thisAlerts:
@KorJun 11.2008 — to correct pass the parameter to the function and to the calculation [I]inside[/I] that function. Functions are the essence of javascript. Avoid using global variables (variables set outside functions), unless it is imperative.

<i>
</i>&lt;script type ="text/javascript"&gt;
function nightsmultiplicetxt(f,name,q) {
var NightPricev = 12;
var WeekPricev = 13;
var MonthPricev = 14;
f[name].value=NightPricev*q;
}
&lt;/script&gt;
...
&lt;form&gt;
&lt;input name="calculate3" type="button" id="calculate3" tabindex="2" onclick="nightsmultiplicetxt(this.form,'nightprice',50)"&gt;
...
&lt;/form&gt;
×

Success!

Help @XTREEMMAK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...