# Inserted html using javascript function

**URL:** <https://discuss.rubyonrails.org/t/inserted-html-using-javascript-function/51561>\
**Category:** rubyonrails-talk\
**Created:** [May 11, 2010, 5:18pm UTC](https://discuss.rubyonrails.org/t/inserted-html-using-javascript-function/51561 "2010-05-11T17:18:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ES1](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@ES1](https://discuss.rubyonrails.org/u/ES1)\
**Post date:** [May 11, 2010, 5:18pm UTC](https://discuss.rubyonrails.org/t/inserted-html-using-javascript-function/51561/1 "2010-05-11T17:18:45Z")

</div>

I'm using a javascript function to insert html code into my page (following the Railscast on nested models)

The function that inserts the javascript is this:

function add\_result(link, association, content) {

&nbsp;&nbsp;var new\_id = new Date().getTime(); &nbsp;&nbsp;var regexp = new RegExp("new\_" + association, "g"); &nbsp;&nbsp;var linkrow = $(link).up("tr");

&nbsp;&nbsp;linkrow.insert({ &nbsp;&nbsp;&nbsp;&nbsp;after: content.replace(regexp, new\_id) }); }

And this is the html.erb that is inserted:

&nbsp;&nbsp;\<% for c in @picture\_fields %\>

&nbsp;&nbsp;&nbsp;&nbsp;\<% if c == "intervention" %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class="pictureclass"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td class="name"\>\<%=t 'form.results.picture.'+c%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= f.radio\_button c, t('form.results.picture.interventionoptions.option1')%\>\<%=t 'form.results.picture.interventionoptions.option1'%\>\<%= f.radio\_button c, t('form.results.picture.interventionoptions.option2')%\>\<%=t 'form.results.picture.interventionoptions.option2'%\>\<%= f.radio\_button c, t('form.results.picture.interventionoptions.option3')%\>\<%=t 'form.results.picture.interventionoptions.option3'%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% elsif c == "couchetype" %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class="pictureclass"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td class="name"\>\<%=t 'form.results.picture.'+c%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= f.text\_field :selected\_couchetypes, :readonly=\>true %\>\</

> 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td class="more"\>\<%= link\_to( image\_tag('more.png', :border=\>0, :id=\>"new\_results\_pictures\_results\_pictures\_couchetype"), {:controller=\>"thesaurus", :action=\>"list", :id=\>"new\_results\_pictures\_results\_pictures\_couchetype"}, { :popup=\>['thesaurus', 'height=800,width=750,location=no,scrollbars=yes']}) %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%else %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class="pictureclass"\>\<td\>\</td\>\<td \>\</td\>\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class="pictureclass"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td class="name"\>\<%=t 'form.results.picture.'+c%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= f.text\_field c, :class=\>"number"%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\>

&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class="pictureclass"\>\<td\>\</td\>\<td\>\<span class="right"\>\<%= f.hidden\_field :\_destroy %\>\<%= link\_to\_function t('form.results.picture.destroy'), "remove\_fields(this, '.pictureclass')"%\>\</span\>\</td\>\</tr\>

When the html is inserted, there is a size="30" attribute for the text input field:

\<input type="text" size="30" name="sample[treatments\_attributes][0] [results\_pictures\_attributes][1273597816957][couchenum]" id="sample\_treatments\_attributes\_0\_results\_pictures\_attributes\_1273597816957\_couchenum" class="number"\>

But I have no idea how it got there. Any ideas?
