RJS: check existing page element?

I have a text box with id "foo" that sometimes exists and sometimes not. If it exists, I would like to fill in a specific value.

How do you do this using RJS (in Rails 2.2)?

I tried this and it does not work:

if page[:foo]
  page[:foo].value = "bar"
end

I get

TypeError: Null Value if the text field does not exist.

.

+3
source share
2 answers

I have met this many times when I have a conditional element that I need to act on if it exists. Using page.select avoids the Null Value error.

  page.select('#dom_object_id').each do |element|
    element.value = "bar"
  end

*.js.rjs. , TypeError, , DOM .

+6

, RJS. javascript- RJS, , .

page << "update_foo('#{my_value}');"

Javascript..

function update_foo(val)
{
  if( $('foo') != undefined )
  {
    $('foo').value = 'bar';
  } 
}

​​ .

+1

Source: https://habr.com/ru/post/1704500/


All Articles