/    Sign up×
Community /Pin to ProfileBookmark

I want a more efficient way of coding to not display buttons

I have two grids, they both display buttons. One grid displays numbers, true or false and yes or no, and the other grid displays letters, true, false, yes and no.

The second grid is not displayed in the code (used css to not display second grid buttons (.answerBtns)) Now using the getButtons() function, if the user selects button “1” in first grid (the grid which you have to open using (Open Grid) link, then it should display button “A” in second grid, if user selects button “2” in first grid, then it should displays buttons “A” and “B” in second grid, if “3” then display “A”, “B” and “C” and so on.

Now except using if statements and stating which buttons should be displayed and not displayed depending on the button chosen in first grid, is there a more efficent way of coding this so that the display of buttons in second grid depends on what is selected in the first grid?

If it is using an array can somebody show a sample of this in their answer. You can just do it for one example and then I should be able to use that to fill it for the other buttons.

Thank you

Code is in jsfiddle, click [here][1]

[1]: [url]http://jsfiddle.net/m2aP2/7/[/url]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JunkMaleDec 10.2011 — Sorry but I am struggling to understand your objective here.

I have looked at the code and its a mix of that stuff I hate and your code which you can dispense with the [code=html]onclick="javascript:buttonclick(this);" [/code]and just go straight for the [code=html]onclick="buttonclick(this);"[/code]and its ID's that need to be unique, therefore [code=html]name="btn8Name"[/code] should be [code=html] id="btn8Name"[/code] for all input elements, simply naming your button elements [code=html]name="btnNumbers"[/code] would give you an array btnNumbers that you can access as an array in javascript.

This [CODE]document.[COLOR="Red"]getElementById[/COLOR]("answer" + letter).className = "answerBtnsOff";[/CODE] is not used when getting [B]<input [/B]elements with a name tag that has unique ID's, you use [B]getElementByName()[/B] to get the element. This is why I said that the input element name is an array and you use name and access it, eg...[code=html]<input class="gridBtns" name="btnName" id="btn15" type="button" value="22" onclick="buttonclick(this);"/>
<input class="gridBtns" name="btnName" id="btn16" type="button" value="23" onclick="buttonclick(this);"/>
<input class="gridBtns" name="btnName" id="btn17" type="button" value="24" onclick="buttonclick(this);"/>
<input class="gridBtns" name="btnName" id="btn18" type="button" value="25" onclick="buttonclick(this);"/>
<input class="gridBtns" name="btnName" id="btn19" type="button" value="26" onclick="buttonclick(this);"/>[/code]
will have a set of buttons that are accessed via an array of elements, to grab all input elements called btnName you use [B]document.getElementsByName("btnName"); [/B]and then access the properties of the object / array.

I think that your making excess work and a bit more design planning and creation of say a [B]name="keypadnumbers"[/B] group for example for the numbers 0 to 9 and so on.... This mix of getting elements by ID and NAME is confusing you as is the way NAME and ID are used.

I hope this helps you,

good luck.
×

Success!

Help @carlbrooks 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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