0

Using javascript and a text link to submit a form?

This is working fine in firefox and IE, but fails when previewing the app inside Yahoo. Any suggestions?

<script>
function $(el) {
if(typeof el == 'object') return el;
return document.getElementById(el);
}
</script>

<form id="search-form" name="ws" method="GET" action="#" />
...
<a href="#" onclick="$('search-form').submit();">Submit</a>
...
</form>

by
2 Replies
  • Mike, see how to register event handlers here:
    http://developer.yahoo.com/yos/code_exs/re...t_handlers.html

    In YAP, you can't use the onclick attribute.
    0
  • QUOTE (Jared @ May 8 2009, 10:28 AM) <{POST_SNAPBACK}>
    Mike, see how to register event handlers here:
    http://developer.yahoo.com/yos/code_exs/re...t_handlers.html

    In YAP, you can't use the onclick attribute.


    That's not entirely true, you can use basic inline event handlers like onclick="", but you couldn't do this:
    $('el).onclick = myfunction;

    However, if we ajax'ed the content into the page, then any inline handlers would be stripped out, and then it wouldn't work as you said. The best thing is to use advanced event handlers like this:

    CODE
    function $(el) {
    if(typeof el == 'object') return el;
    return document.getElementById(el);
    }

    function $add_event(el, eventtype, callback) {
    el = $(el);
    if(el==null) return false;
    if(el.addEventListener){
    el.addEventListener(eventtype, callback, false);
    }
    else if(el.attachEvent){
    el.attachEvent("on"+eventtype, callback);
    }
    return true;
    }

    //example
    $add_event('elementid', 'click', myfunction);
    0

Recent Posts

in YAP