Forum Moderators: open

Message Too Old, No Replies

adding php function to JS file

select navigation into list - need onchange function added

         

ivanji

9:12 pm on Mar 20, 2009 (gmt 0)



Hi - i'm wondering if anyone could help me out with this,

I have this JS code which converts select options into lists in order to make them look nice - as everyone knows select lists are ugly by default.

I only need to add this onchange function (which contains some php code) to the li elements:

onchange="if (this.selectedIndex > 0) location.href=this[this.selectedIndex].value;"

I need to add the onchange function to give something to do to these lists because as they are they're currently useless.

Please help, any comments welcome - if you need more info please let me know.

thanks!

[edited by: coopster at 1:05 pm (utc) on Mar. 21, 2009]
[edit reason] removed code dump and urls [/edit]

coopster

1:07 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, ivanji.

Please take a moment to read the Javascript Forum Policies for Links and Code [webmasterworld.com].

Where exactly are you having difficulty? In the PHP code? Or are you wondering how to attach the onchange event handler?

ivanji

1:15 pm on Mar 21, 2009 (gmt 0)



Thanks for your reply and message.

I'm just having difficulty adding the onchange event to the JS file. Is it possible to do?

Any help is appreciated. Thanks

coopster

1:33 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure. You just need to define the onchange event handler for your element. For example, if an <li> element had an id attribute with a value of "myLI" you would do something like this, using your specifications ...
document.getElementById('myLI').onchange = function() 
{
if (this.selectedIndex > 0) location.href = this[this.selectedIndex].value;
};

ivanji

2:00 pm on Mar 21, 2009 (gmt 0)



Thanks for your reply. That sounds like good news.

So in this case you mean I'd need to place this not in the JS file but in the file that has the markup or between the <head> tags? Sorry JS isn't my forte...

coopster

2:24 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, either that or in an external JavaScript file that monitors for an "onload" event and put it into the onload initialization process.

rocknbil

2:48 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it's not the li that reacts to the onchange, it's the select itself.

Furthermore, you can make this code anonymous so all you have to add is the relevant list to onload so any list can access it. So lets say you have two "jump" menus, (which is what you're building with window.href) one at the top of the page, one at the bottom:

<form method="post" action="yourscript.php" id="form1">
<select name="jump" id="jump_top">
<option value="">Select</option>
.....
</select>
<input type="submit" value="Go">
</form>
....

<form method="post" action="yourscript.php" id="form2">
<select name="jump" id="jump_bottom">
<option value="">Select</option>
.....
</select>
<input type="submit" value="Go">
</form>

Note that name is the same in both lists, so your php script only has to look for $_POST['jump']*, not two separate input values. Alternatively, you can make your selects more accessible: that is, if Javascript is disabled, location/href won't work at all. So instead, add a submit button and submit the form onchange of the lists.

Your javascript, whether external or in the head section, might look like this:

*By using post, you don't have the ugly query string in the address bar.


window.onload=function() { attachBehaviors(); }
function attachBehaviors() {
if (document.getElementById) {
document.getElementById('top_menu').onchange=function() { jumpMenu(this,this.form); }
document.getElementById('bottom_menu').onchange=function() { jumpMenu(this, this.form); }
document.getElementById('form1').onsubmit=function() { return selectList(this); }
document.getElementById('form2').onsubmit=function() { return selectList(this); }
}
}
function jumpMenu(selectObject, form) {
// if the top '' value is selected, do nothing.
// This allows the user to "reset" the list
var ind = selectObject.selectedIndex;
if (ind > 0) {
form.submit();
}
}
// If javascript is enabled, the form will submit
// onchange of the list. So if they hit submit
// when nothing is selected, just let 'em know.
// By returning false, this stops the form submit.
function selectList(form) {
alert('Please select an item from the list.');
return false;
}

This still has a "hole" in it - if the user is very fast, they could potentially select from the list and hit the submit, which would give them the alert. To eliminate this possibility, you could put the submit in a div or paragraph and set the innerHTML to '' onload.