views:

46

answers:

2

Hi, I have this code:

<input name="rz" class="required validate-string" style="margin-left:17px"      id="rz" title="Input rz value" size="23" />
<p class="msg" style="display:none;">Input rz value</p>

In the head I have:

Event.observe(window, 'load', function() {

$$("input").each(function(field){
  Event.observe(field, "focus", function(input) {
    input.adjacent('p.msg').show();
  });
  Event.observe(field, "blur", function(input) {
    input.adjacent('p.msg').hide();
  });
});

});

The idea is that when the input get the focus, the p element appears and on blur it goes away. The problem is that neither is working, and the error console shows "input.adjacent is not a function"

I'm using prototype 1.6.1 and scriptaculous 1.8.3

A: 

Argument passed to event handler (i.e. function passed to Event.observe function) is event object, not target element. Element to which event handler is bound is passed as "this" pointer, so you should use:

this.adjacent('p.msg').hide();
el.pescado
Thanks for your reply, with the change you suggested, I get the "this.adjacent("p.msg").show" is not a function" and "this.adjacent("p.msg").hide" is not a function" errors in the console.
xain
You should really read the docs first. adjacent() returns list of adjacent elements, not adjacent element itself. Try this.adjacent('p.msg')[0].hide() if you know that ther is just one adjacent element, or use each() function to call code on every element in list.http://www.prototypejs.org/api/element/adjacent
el.pescado
+1  A: 

What el.pescado says is true.

Perhaps you should be using .next() or .previous() instead of .adjacent() if you want to get the item beside the element triggering the event.

Diodeus