RJS to change image on submit button

Hi I have an observer that fires on the basis of a drop-down.

I need to change the icon on the submit button of a form based on this
value. I created the submit button using “image_submit_tag”.

I have it working with this code in my RJS template:

if params[:type] == “type_a”
page.replace_html “submit_button”,
‘’
else
page.replace_html “submit_button”,
‘’
end

I expect there is a better way to do this than having to replace the
code generated by my image_submit_tag. I would rather just replace the
image directly.

Any help/ideas appreciated…

On 23 Mar 2008, at 13:35, John L. wrote:

page.replace_html “submit_button”,
'<input class=“submit” src="/images/btn_type_a.gif" type=“image” /


else
page.replace_html “submit_button”,
‘’
end

You could always just do page << “$
(‘submit_button’).src=‘someother_picture.gif’”;

or even page[‘submit_button’].src= ‘someother_picture.gif’ (which will
result in the same output).

Fred

John L. wrote:

if params[:type] == “type_a”
page.replace_html “submit_button”,
‘’
else
page.replace_html “submit_button”,
‘’
end

I expect there is a better way to do this than having to replace the
code generated by my image_submit_tag. I would rather just replace the
image directly.

The following springs to mind:

page << “$$(‘input.submit’)[0].src = ‘/images/btn_#{params[:type]}.gif’”

Assuming that params[:type} always maps into the image name.

Untested, but I’ve done similar things before…

Frederick C. wrote:

or even page[‘submit_button’].src= ‘someother_picture.gif’ (which will
result in the same output).

Fred

Thanks Fred. Sometimes things are blindingly obvious :slight_smile: I’ve used your
suggestion and it’s made for cheaner code. Many Thanks.