Login | Forgot password | Register

X

What is OpenID?

OpenID is an Internet-wide identity system that allows you to sign in to many websites with a single account.

With OpenID, your ID becomes a URL (e.g. http://username.myopenid.com/). You can get a free OpenID for example from myopenid.com.

For more information visit the official OpenID site.

Jump to:

17480 Posts in 4474 Topics by 1973 members

Archive

SilverStripe Forums » Archive » adding onclick to search form button

Our old forums are still available as a read-only archive.

Page: 1
Go to End
Author Topic: adding onclick to search form button 1010 Views
  • Nicolaas
    avatar
    Community Member
    136 posts

    adding onclick to search form button Link to this post

    Hi Folk

    I have written the following piece of code (from tutorial):
    class Page_Controller extends ContentController {
    function SearchForm() {
    $searchText = isset($this->Query) ? $this->Query : 'Search';
    $searchField = new TextField("Search", "", $searchText);
    $fields = new FieldSet($searchField);
    $resultAction = new FormAction("results","Go");
    $actions = new FieldSet($resultAction);
    return new SearchForm($this, "SearchForm", $fields, $actions);
    }

    I would like to add some JavaScript to the search field so that it automatically removes the word search onclick.

    It seems like I can only do this in Sapphire or is there another way. I am not too keen to do it in Sapphire, because then it will get lost when I upgrade the site.

    Any help greatly appreciated.

    Nicolaas

  • Sean
    avatar
    Core Development Team
    482 posts

    Re: adding onclick to search form button Link to this post

    If you include the behaviour script as requirements for your class you can create a new js file with something like this:

    eg. Page.php

    class Page_Controller extends ContentController {

    function init() {
    Requirements::javascript('jsparty/behaviour.js');
    Requirements::javascript('mysite/javascript/SearchForm.js');
    parent::init();
    }

    }

    And then, you can create your SearchForm.js file (Replace the # ID with the correct search input field ID if this one isn't correct:

    Behaviour.register({
    '#Search_Form_SearchForm_Search' : {
    onfocus : function() {
             if(this.value == 'Search') this.value = '';
    },
    onblur : function() {
       if(this.value == '') this.value = 'Search';
    }
    }
    });

    Hope this helps!

    Cheers,
    Sean

  • Nicolaas
    avatar
    Community Member
    136 posts

    Re: adding onclick to search form button Link to this post

    Hi Sean

    Thank you for your reply.

    I noticed that all my pages have the the following JS attached (added to my source code) by default:

    <script type="text/javascript" src="jsparty/behaviour.js"></script>
    <script type="text/javascript" src="jsparty/prototype.js"></script>
    <script type="text/javascript" src="jsparty/scriptaculous/effects.js"></script>
    <script type="text/javascript" src="cms/javascript/PageCommentInterface.js"></script>

    Why would that be? It is not in the template/age.ss file and not in the code/page.php file, that is for sure....

    The behaviour does not really seem to work either.... (but not JS errors).

    Any suggestions?

  • Sean
    avatar
    Core Development Team
    482 posts

    Re: adding onclick to search form button Link to this post

    Those files will have been included because you've enabled the page commenting. There's some javascript for ajax on the comments interface that depends on behaviour and prototype.

    Cheers,
    Sean

    1010 Views
Page: 1
Go to Top

Currently Online: Webdoc

Welcome to our latest member: AlfonsoGrondo

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.