Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

OBJECT EXPECTED WHEN I USE STRUTS IN INNERHTML

Hello, i began to use javascript recently and I'm receiving an javascript error "OBJECT EXPECTED" when i test my function:

function inserirlinha()
            {
                alert("entrei na função inserir linha.");
                var table=document.getElementById('tabelaLigacoesMultiplas');
                alert("tabela = "+table);
                var newRow=table.insertRow(table.rows.length);
                alert("nº linhas da tabela = "+table.rows.length);
                alert("table.rows[0].cells.length");
                for(var i=0;i<table.rows[0].cells.length;i++){
                   alert("entrei no ciclo for");
                   var cell=newRow.insertCell(i);
                   switch (i) {
                     case 0: {
                      cell.className="hand";
                      cell.align="center";
                      cell.onclick="eventOnload();";
                      cell.innerHTML="<img src='./images/right.gif'>";
                      alert("i ="+i);  
                      break;
                     }                      
                     case 1: {                                    
                      //cell.innerHTML="<textarea name='teste' id='teste' width='50px'></textarea>";
                      cell.innerHTML="<html:form action='/LigacoesMultiplasPorCamada.do'    
target='iFrameTabelaLigacoesMultiplasPorCamada'><html:text name='formLigacoesMultiplasCamadaIFrame' property='nomeLigacao' styleClass='SubFormTextbox' maxlength='10' disabled='true'></html:text></html:form>";
                      //cell.innerHTML="<html:text name='formLigacoesMultiplasCamadaIFrame' property='nomeLigacao' styleClass='SubFormTextbox' maxlength='10' disabled='true'></html:text>";
                     // alert("i ="+i);
                     // alert("cell.innerHTML");
                     // break;                    
                     }
                     default:
                      alert("Em desenvolvimento");
                   } //switch  
                } //for        
            } //inserirlinha


The error occurs at line : "cell.innerHTML="<html:form action='/LigacoesMultiplasPorCamada.do' ....."~

Can anyone help me with this?

Thanks,
Nuno
0
NUNOPALMA
Asked:
NUNOPALMA
  • 4
  • 4
  • 2
  • +1
1 Solution
 
smidgie82Commented:
Hi NUNOPALMA,

What you need to do instead of trying to manipulate the innerHtml element, is use Javascript to manipulate the DOM.  For instance:

var parent_obj = document.getElementById('the_cell')
var new_obj = document.createElement('form')
new_obj.target='iFrameTabelLigacoesMultiplasPorCamada'
new_obj.action='/LigacoesMultiplasPorCamada.do'
parent_obj.appendChild(new_obj)

parent_obj = new_obj
new_obj = document.createElement('text')
...
parent_obj.appendChild(new_obj)


Cheers!
0
 
enachemcCommented:
The line "cell.innerHTML="<html:form action='/LigacoesMultiplasPorCamada.do' ....."~ will by transformed by the JSP and will look different in the transformed HTML code, perhaps it would span multiple lines (but you define it in a string that does not span multiple lines). To see exactly what happens, use the HTML generated by the JSP (View Source from your browser).
0
 
NUNOPALMAAuthor Commented:
I tryed to manipulate the DOM as smidgie82 told me and now i have a new javascript error:

Unexpected call to method or property access at line " var new_obj = document.createElement("html:form")"

"html:form" is a struts tag

Here is my new function:

function inserirlinha()
            {
                     
                alert("entrei na função inserir linha.");
                var table=document.getElementById('tabelaLigacoesMultiplas');
                alert("tabela = "+table);
                var newRow=table.insertRow(table.rows.length);
                alert("nº linhas da tabela = "+table.rows.length);
                alert("table.rows[0].cells.length");
                for(var i=0;i<table.rows[0].cells.length;i++){
                   alert("entrei no ciclo for, i="+i);
                   var cell=newRow.insertCell(i);
                   switch (i) {
                     case 0: {  
                      //cell.id="the_cell";            
                      cell.className="hand";
                      cell.align="center";
                      cell.onclick="eventOnload();";
                      cell.innerHTML="<img src='./images/right.gif'>";
                      alert("i ="+i);  
                      break;
                     }                      
                     case 1: {
                     //cell.id="the_cell";
                     alert("i ="+i);
                     var parent_obj = cell    
                     var new_obj = document.createElement("html:form")
                     new_obj.target='iFrameTabelLigacoesMultiplasPorCamada'
                     new_obj.action='/LigacoesMultiplasPorCamada.do'
                     parent_obj.appendChild(new_obj)

                     parent_obj = new_obj
                     new_obj = document.createElement('textarea')
                     new_obj.name='teste'
                     new_obj.id='teste'  
                     new_obj.styleClass='SubFormTextbox'
                     parent_obj.appendChild(new_obj)                              
                     alert("i ="+i)
                     alert("new_obj ="+new_obj)
                     break;                    
                     }
                     default:
                      alert("Em desenvolvimento");
                   } //switch  
                } //for        
            } //inserirlinha
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
enachemcCommented:
This is because html:form is not an HTML tag, it is an Struts tag. This tag will be interpreted by Struts and will output HTML tags. You probably should use the HTML "form" tag.
0
 
NUNOPALMAAuthor Commented:
I tried to use the HTML "form" tag instead of the Struts "html:form" tag and i receive no error but it doesn't display the textbox in the screen

Here it is my changed code:

....

case 1: {
                     alert("i ="+i);
                     var parent_obj = cell     //document.getElementById('the_cell')
                     var new_obj = document.createElement("form")
                     new_obj.target='iFrameTabelLigacoesMultiplasPorCamada'
                     new_obj.action='/LigacoesMultiplasPorCamada.do'
                     parent_obj.appendChild(new_obj)

                     parent_obj = new_obj
                     new_obj = document.createElement('text')
                     new_obj.name='formLigacoesMultiplasCamadaIFrame'
                     new_obj.property='nomeLigacao'
                     new_obj.styleClass='SubFormTextbox'
                     new_obj.maxlength='10'  
                     new_obj.disabled='false'
                     parent_obj.appendChild(new_obj)
                     alert("i ="+i)
                     break;                    
                     }

Thanks,
Nuno
0
 
enachemcCommented:
new_obj = document.createElement('text')

shouldn't this be  
new_obj = document.createElement('input');
new_obj.type="text";

?
0
 
gops1Commented:
Even if you are struts, finally when it is rendered in a page it becomes a normal HTML tags, so do not use the struts tag, use simple HTML tags.
0
 
smidgie82Commented:
enachemc,

You're absolutely right.  My mistake.  

document.createElement('text') line should be replaced by new_obj = document.createElement('input'); new_obj.type = 'text';

Good call.

~Dathan
0
 
NUNOPALMAAuthor Commented:
thank you enachemc, you solved my problem. but instead of
new_obj = document.createElement('input');
we must use
new_obj = document.createElement("input");

0
 
enachemcCommented:
why ?
0
 
NUNOPALMAAuthor Commented:
i don't know. I just tryed and it resulted with " "
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now