Skip to main content
Topic: Adding Buttons - supply an onclick function? (Read 5875 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Adding Buttons - supply an onclick function?

Is there any way short of writing custom code to add a new button to the button array that has an onclick script instead of a link?  Or since the buttons with sub_buttons are very much select like, is there a way to make one actually be a select?  Meaning you select the option from the drop down and it changes the button name and passes a value in on submit?
Last Edit: March 06, 2014, 10:06:19 am by scripple

Re: Adding Buttons - supply on onclick function?

Reply #1

It'd be possible, but with the proviso that you couldn't make it part of the array as such, AFAIK. The thing is the array is set up to deal with anchors, not onclick. What I think you would have to do is add a button outside the array, like the header upshrink. That uses onclick and is styled to look like the other buttons, but is not part of the array in Sources.

Same applies for using selects.
Master of Expletives: Now with improved family f@&king friendliness! :D

Sources code: making easy front end changes difficult since 1873. :P

Re: Adding Buttons - supply on onclick function?

Reply #2

Perhaps the array could have an id field, then a script could be attached via an external script assigning an onclick function to a button with that id.  This would be a future change though as I don't think the array holds IDs.  Or is selecting on an href doable and reasonable?

Re: Adding Buttons - supply on onclick function?

Reply #3

Buttons already have id's that depend on the actions they perform. So yes, you could do it that way I suppose. I hadn't thought it through.

For instance, the "Messages" button is #button_pm. As long as an action is defined for the button, it'll get a suitable id.
Master of Expletives: Now with improved family f@&king friendliness! :D

Sources code: making easy front end changes difficult since 1873. :P

Re: Adding Buttons - supply on onclick function?

Reply #4

It seems sub-buttons don't have id's though.  Can they if the right field is set in the array?

Re: Adding Buttons - supply on onclick function?

Reply #5

Dunno. Never tried it. I suppose the back end code could be tweaked so they did, if it's not allowed already. I'd suggest adding an id on a test site and seeing what happens.
Master of Expletives: Now with improved family f@&king friendliness! :D

Sources code: making easy front end changes difficult since 1873. :P

Re: Adding Buttons - supply on onclick function?

Reply #6

Yeah I'll have to look at the array and see what field needs to be added.

Edit:  Didn't see an obvious tag in the array that creates the id on main buttons vs sub-buttons.
Last Edit: March 06, 2014, 01:54:28 am by scripple

Re: Adding Buttons - supply on onclick function?

Reply #7

Maybe use the key of the array?
Code: [Select]
foreach ($button['sub_buttons'] as $child_act => $childbutton)
Code: [Select]
foreach ($childbutton['sub_buttons'] as $grandc_act => $grandchildbutton)
Can't check right now, if you confirm it works I can add it later. ;)
Bugs creator.
Features destroyer.
Template killer.

Re: Adding Buttons - supply on onclick function?

Reply #8

Works to add ids to childbuttons, not sure what buttons have grandchild buttons and have a meeting to rush off to this morning so can't do anything more but thanks for this.

Re: Adding Buttons - supply an onclick function?

Reply #9

Ok, I gave it a try and noticed the admin menu has 3rd level buttons.  I decided to make the ids be button_$act_$child_act, and button_$act_$child_act_$grandc_act as the odds of a name collision at that point are lower than just using one of the subvariables.

I then ran the main page through an html5 validator and it was fine.  Here's the modified code secion.

Code: [Select]
		// Any 2nd level menus?
if (!empty($button['sub_buttons']))
{
echo '
<ul class="menulevel2">';

foreach ($button['sub_buttons'] as $child_act => $childbutton)
{
echo '
<li id="button_', $act , '_', $child_act, '" class="listlevel2', !empty($childbutton['sub_buttons']) ? ' subsections" aria-haspopup="true"' : '"', '>
<a class="linklevel2" href="', $childbutton['href'], '" ', isset($childbutton['target']) ? 'target="' . $childbutton['target'] . '"' : '', '>', $childbutton['title'], '</a>';

// 3rd level menus :)
if (!empty($childbutton['sub_buttons']))
{
echo '
<ul class="menulevel3">';

foreach ($childbutton['sub_buttons'] as $grandc_act => $grandchildbutton)
echo '
<li id="button_', $act, '_', $child_act, '_', $grandc_act, '" class="listlevel3">
<a class="linklevel3" href="', $grandchildbutton['href'], '" ', isset($grandchildbutton['target']) ? 'target="' . $grandchildbutton['target'] . '"' : '', '>', $grandchildbutton['title'], '</a>
</li>';

echo '
</ul>';
}

Haven't attached a script to any of them yet but I don't see why it would be a problem.

Re: Adding Buttons - supply an onclick function?

Reply #10

In theory there shouldn't be id collision (any identical key name should be avoided because it could be a problem in future possible menu editors O:-)).
The problem with a hierarchical structure is that the moment the position of the menu is changed (for whatever reason) you have to change the the javascript too. If that is acceptable, okay we can use that. I would prefer the "simple" one (also because the IDs generated are looooooong).
Let me know. ;D
Bugs creator.
Features destroyer.
Template killer.

Re: Adding Buttons - supply an onclick function?

Reply #11

I prefer the short form too, I just wasn't sure since you hadn't been setting id's on the sub_buttons if there was any confidence they would be unique.  Since you're happier with the short form I say definitely go that way.  Especially with your point about what happens if a button moves.

Re: Adding Buttons - supply an onclick function?

Reply #12

And if you notice any duplicate id, feel free to report it as bug! ;D
Bugs creator.
Features destroyer.
Template killer.

Re: Adding Buttons - supply an onclick function?

Reply #13

So it seems this never made it into the baseline.  Should I assume it's been rejected?

Re: Adding Buttons - supply an onclick function?

Reply #14

More likely it slipped out of sight... O:-)
Bugs creator.
Features destroyer.
Template killer.