how do i pass javascript to a dropdown?

Hi

I want my page to reload when user selects a value from a dropdown. but when i pass 'js' => 'onlclick=county_reload()' as one of parameters to dropdown view it appears in the select tags, not the option tags. So onclick doesnt fire. is it possible to pass JS to dropdown options?

Thanks

Mark

  • Oops, my fault, passing onchange() to the dropdown works

  • The core team is discouraging the use of inline js, as it might be deprecated in one of the next major releases.

  • Deprecated or not, it's best practices to use bound events in JS instead of on* functions. jQuery makes this fairly painless. $('#my-selector').change(callback); Here's a good resource on jQuery's site about that: http://docs.jquery.com/Tutorials:AJAX_and_Events

  • I'll have a look at that jquery stuff. is the simplest way to do it to get the javascript to reload the page, passing the variable from the drop down through the url? or is this way old-fashioned now??

    i would like to do some ajax so page doesnt reload. but im not very familiar with it and i dont know how hard it is. are there any simple examples of elgg plugins that use ajax to reload entities based on new metadata without reloading page?

  • If you're changing data you should use POST instead of GET (variables passed through the URL) in your form. Google has lots more info, but the basic rule of thumb is if you change data in the DB, use POST.

    The message board plugin does some ajax in its widget. You post a message and in the save action it returns only the HTML to add the new post on the board. That might get you started.

  • Hi Brett

    I wont be changing data, im using the parameters selected by the user to filter the entities. So GET should be ok for that. Ill have a look at the message board plugin to see how ajax works

  • Ok. I was confused by this question:

    are there any simple examples of elgg plugins that use ajax to reload entities based on new metadata without reloading page?

    It makes it sound like you're changing metadata on an entity.

  • sorry for the confusion. nope, not changing any data, only getting the entities again with the parameters specified by users. i'll use GET and park the ajax for now