Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:15:03+00:00 2026-06-07T14:15:03+00:00

I am using Prototype JavaScript, I have a button and I want to use

  • 0

I am using Prototype JavaScript, I have a button and I want to use the Event.observe method to observe for a click action.

The issue is that I want to call one of two functions depending on an if statement in the PHP code. My understanding of Javascript and the Event.observe method is quite limited but it seems like the code doesn’t work when trying to observe for an element that isn’t present. I can get this to work in a very hacky way, as follows:

<?php if ($_type == "forward_second"):?>
    <input type="button" id="week_backward_second" value="&lt;&lt;" disabled/>
    <input type="hidden" id="week_backward" value="&lt;&lt;" />
<?php else: ?>
    <input type="button" id="week_backward" value="&lt;&lt;" disabled/>
    <input type="hidden" id="week_backward_second" value="&lt;&lt;" />
<?php endif; ?>

And in the JavaScript:

initialize: function {    
    Event.observe('week_backward', 'click', this.backward.bind(this));
    Event.observe('week_backward_second', 'click', this.backwardSecond.bind(this));
},

By having a hidden field the ‘unnecessary’ Event.observe has an element to observe and the code works.

Can anyone suggest a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:15:04+00:00Added an answer on June 7, 2026 at 2:15 pm

    I hope that I understood your question 🙂

    var element = $('week_backward'),
        method = this.backward;
    
    // Get a second element if first doesn't exist.
    if (!element) {
        element = $('week_backward_second');
        method = this.backwardSecond;
    }
    
    element.observe('click', method.bind(this));
    

    I used shorter Prototype API – unless you don’t have conflicts with other libs I recommend it.

    PS. I forgot to mention that now you’ll only have to print one button, without the hidden one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JavaScript, is it possible to use prototype to add a method to a
I've read that using Object.prototype to attach functions to custom JavaScript objects is more
Does the Prototype Javascript library have a selector that's equivalent to jQuery's :contains() ?
I am try using prototype javascript framework. i have next code CheckBoxSlider = Class.create({
I have a requirement in Javascript (using Prototype) to focus the cursor on the
Given I have a string - invoice[level1][level2][level3] and I am using Prototype as Javascript
I have a Javascript function with a namespace and I am using Prototype to
I am using prototype and have the following javascript: function displayDefinition(term){ var term_id =
I have JavaScript code that adds and removes classes using the classList object. The
I have created a modal window using the Prototype JavaScript framework and the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.