/    Sign up×
Community /Pin to ProfileBookmark

variable problem

[CODE]
if(this.selection.document.x.checked==true)
document.selection.x.disabled=true;
[/CODE]

selection is the name of the form and type is the name of the checkbox in this html form.What I want is to know if that checkbox is checked or not by use of javascript.However the problem is the name of the checkbox is dynamic.So I should write a dynamic statement in javascript to get the variable.
in PHP code I assigned the dynamic name of the chechbox into variable x.
However in javascript when I write “this.selection.document.x.checked==true”,
it doesnt work because it recognizes x as x.However I want it to recognize x as the dynamic value that I got.Let’s say the value of x is “checkbox1val”.
I want to mention “chechbox1val” when I write “this.selection.document.x.checked==true”, not x itself.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoMar 28.2007 — document.all[x].checked==true
Copy linkTweet thisAlerts:
@KorMar 28.2007 — document.all[x].checked==true[/QUOTE]
Wrong, this is an old deprecate IE only method!

Don't use document.all.

[B]1.[/B] Use DOM methods:

[COLOR="Blue"]document.getElementsByName(x)[[I]index[/I]].checked[/COLOR]

If you have a single element, the [I]index[/I] value is [B]0[/B], otherwise you should circle with a loop

[B]2.[/B] You may also use the classical form's elements square brackets notation

[COLOR="Blue"]document.forms[[I]form_name[/I]].elements[[I]element_name[/I]][/COLOR]

or the short (but make sure you have no frames/iframes with the same name as the form):

[COLOR="Blue"]document[[I]form_name[/I]][[I]element_name[/I]][/COLOR]

Your case:
<i>
</i>document['selection'][x]


Both methods are cross browser.
×

Success!

Help @guess 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...