Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.5K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.8K SQL & PL/SQL
- 21.2K SQL Developer
- 295.3K Development
- 17 Developer Projects
- 138 Programming Languages
- 292K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 27 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 390 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1K Español
- 1.9K Japanese
- 230 Portuguese
AppendChild problem

920164
Member Posts: 47
Hi all
I am adding the following script at the end of my JSP to add a hidden field but it dosent work unless i add an alert statment before appending the new element, dose anyone have any explanation of this strange behavior?
<%
String transactionNo =request.getParameter("transactionNo");
System.out.println("PSS At error footer jsp -----" + transactionNo);
%>
<script language="JavaScript" type="text/javascript">
var inputFeild = document.createElement("input");
inputFeild.type = "hidden";
inputFeild.name = "transactionNo";
inputFeild.value = "<%=transactionNo%>";
var formsCollection = document.getElementsByTagName('form');
// i i add alert here, it works!!!!
for(var i=0;i<formsCollection.length;i++){
document.getElementsByTagName('form')[0].appendChild(inputFeild);
}
</script>
I am adding the following script at the end of my JSP to add a hidden field but it dosent work unless i add an alert statment before appending the new element, dose anyone have any explanation of this strange behavior?
<%
String transactionNo =request.getParameter("transactionNo");
System.out.println("PSS At error footer jsp -----" + transactionNo);
%>
<script language="JavaScript" type="text/javascript">
var inputFeild = document.createElement("input");
inputFeild.type = "hidden";
inputFeild.name = "transactionNo";
inputFeild.value = "<%=transactionNo%>";
var formsCollection = document.getElementsByTagName('form');
// i i add alert here, it works!!!!
for(var i=0;i<formsCollection.length;i++){
document.getElementsByTagName('form')[0].appendChild(inputFeild);
}
</script>
Answers
-
As a test, in stead of executing that javascript at the end of the page, put it in a function and invoke it in the onLoad() handler of the body tag. Does it then work correctly?
Let me explain what my line of thinking is: you are attempting to modify the document before the page is fully loaded - that doesn't seem like a good idea to me. When you trigger this in onLoad(), you are guaranteed to be executing it when the full DOM tree has been created. At the very least, it is a good test.
BTW: there is a JSP forum. Please take the time to pick the proper sub forum for a question. -
Thanks for your reply, i can understand you very well and what you suggested is working very well, but my problem is that i have more than 50 jsps that are already written, and i dont want to touch all o them, instead i searched for a jsp that is included in all o them footer.jsp and added the required script their. do you have any better approach?
-
917161 wrote:I would use jquery's document ready function. You can add that in your footer.jsp. Something like
Thanks for your reply, i can understand you very well and what you suggested is working very well, but my problem is that i have more than 50 jsps that are already written, and i dont want to touch all o them, instead i searched for a jsp that is included in all o them footer.jsp and added the required script their. do you have any better approach?$(document).ready(function(){ // Your code here });
-
Crosspost, and JavaScript question in Java forum on Java site. Off topic. Locking.
This discussion has been closed.