Solved

OBJECT EXPECTED WHEN I USE STRUTS IN INNERHTML

Posted on 2006-07-11
11
699 Views
Last Modified: 2008-01-16
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
Comment
Question by:NUNOPALMA
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:smidgie82
ID: 17083278
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
 
LVL 12

Expert Comment

by:enachemc
ID: 17088410
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
 

Author Comment

by:NUNOPALMA
ID: 17089176
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
 
LVL 12

Expert Comment

by:enachemc
ID: 17089216
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
 

Author Comment

by:NUNOPALMA
ID: 17089317
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Accepted Solution

by:
enachemc earned 500 total points
ID: 17089511
new_obj = document.createElement('text')

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

?
0
 
LVL 17

Expert Comment

by:gops1
ID: 17089916
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
 
LVL 9

Expert Comment

by:smidgie82
ID: 17090504
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
 

Author Comment

by:NUNOPALMA
ID: 17090561
thank you enachemc, you solved my problem. but instead of
new_obj = document.createElement('input');
we must use
new_obj = document.createElement("input");

0
 
LVL 12

Expert Comment

by:enachemc
ID: 17090614
why ?
0
 

Author Comment

by:NUNOPALMA
ID: 17092146
i don't know. I just tryed and it resulted with " "
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now