Solved

Create New Div's with styles inside the Div

Posted on 2011-09-15
14
279 Views
Last Modified: 2012-05-12
My Question here, I am using Javascript Modal Window an Old Modal Window of Javascript not using the jquery!

Now i can get the data from the modal window bt when i clik "Add", it does not populate my DIV which i have declared inside the Page!

so here is the code

with this code i am getting error
theDiv.appendChild is not a function
Line 11
function getMyValues(){
	var show = window.showModalDialog("abc.cfm", window,"center=yes;dialogWidth:900px:dialogHeight:800px"); 
	if(show!=undefined) { 
		var aid = show.SID;
		var arrID = aid.split(',');
		for (var j=0; j<arrID.length; j++){
			var theDiv = 'projectsList'; -- This is my parent Div name
			var newNode = document.createElement('div');  -- I am trying to create a New Div inside the Div of the parent with the Styles, also want to pass asa hidden field with the each div which is being created inside the parent div
			newNode.innerHTML = arrID[j]; 
			theDiv.appendChild(newNode) 
		}
		
	}
}

Open in new window

0
Comment
Question by:myselfrandhawa
  • 8
  • 6
14 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
var theDiv = document.getElementById('projectsList'); // -- This is my parent Div ID
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
ok it worked, but it added only 1 record and neither the style attribute if i need to have something like

a border to doiv with width of 100 and an image icon if possible so when clicked will delete that DIV tag from the parent div

Please guide me

Thanks for the Prompt response mplungjan
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Not sure I follow

You mean something like this
function getMyValues(){
  var show = window.showModalDialog("abc.cfm", window,"center=yes;dialogWidth:900px:dialogHeight:800px"); 
  if(show!=undefined) { 
    var aid = show.SID;
    var arrID = aid.split(',');
    var theDiv = document.getElementById('projectsList');
    alert('adding ' + arrID.length + ' elements'); // what does this alert?
    for (var j=0; j<arrID.length; j++){
       // I am trying to create a New Div inside the Div of the parent with the Styles, 
       // also want to pass as a hidden field with the each div which is being created inside the parent div
      var newNode = document.createElement('div');
      newNode.style.border='1px solid black';  
      newNode.innerHTML = arrID[j] + '<img src="somegif.gif" onclick="var el = this.parent; el.parent.removeChild(el)" />'; 
      theDiv.appendChild(newNode); 
    }
    
  }
}

Open in new window

0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
yes the way i am doing is bit different but same as above

i am using the following as:

var newNode = document.createElement('div');
      newNode.style.border='1px solid black';  
      newNode.style.textDecoration = 'none';

now the above breaks if i use something like this

var newNode = document.createElement('div');
      newNode.style.border='1px solid black';  
      newNode.style.textDecoration = 'none !important';

using !important because globally a has underline and i want to remove underline from here

0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
i am getting an error in second approach
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
1. What error?
2. Why set text-decoration on a div and not on the links in the div? Set the className and use a style sheet
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
if i declare the classname in my main global.css file, will it get it or i have to declare it just above where i am calling the function because i am making the function inside the JS File
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
If the class is declared in the CSS and the div is created after the CSS has loaded, the div will get the class.
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
ok i will try and get back to you
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
ok i tried like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function getMyValues(){        
	var show = window.showModalDialog("abc.cfm", window,"center=yes;dialogWidth:900px:dialogHeight:800px");         
		if(show!=undefined) {                 
			var aid = show.SID;                
			var arrID = aid.split(',');                
				for (var j=0; j<arrID.length; j++){                        
				var theDiv = 'projectsList'; // This is my parent Div name                        
				var newNode = document.createElement('div');  //I am trying to create a New Div inside the Div of the parent with the Styles, also want to pass asa hidden field with the each div which is being created inside the parent div   
				newNode.className = "classfile";                     
				newNode.innerHTML = arrID[j] + '<imgs src=img.png onclick=$(this).remove()>'; // I want that when i click on the image it should remove the whole DIV, currently it is removing only the Image icon, if i try this the code logic which is written                         
				theDiv.appendChild(newNode)                 
				}                        
		}
}
</script>
</head>

<body>
</body>
</html>

Open in new window

0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
please guide
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
your onclick is only removing the image

onclick="var el = this.parent; el.parent.removeChild(el)"

would remove the div that contains it.
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
tried ur way!

got the error!

parent is null or not an object IN IE 7

in Mozilla i get this

el is undefined
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
Comment Utility
I missed a node and you missed a getElementById and also $(this) is jQuery and you do not load jQuery in your example
Here is a demo
function getMyValues() {             
    var show = window.showModalDialog("abc.cfm", window,"center=yes;dialogWidth:900px:dialogHeight:800px");                  
    if(show!=undefined) {
      var aid = show.SID;    
      var arrID = aid.split(',');                                 
      var theDiv = document.getElementById('projectsList'); // This is my parent Div name
      for (var j=0; j<arrID.length; j++){                                         
        var newNode = document.createElement('div');   
        newNode.className = "classfile";                                      
        newNode.innerHTML = arrID[j] + '<img src="img.png" onclick="var el=this.parentNode;el.parentNode.removeChild(el)">';          
        theDiv.appendChild(newNode);
      }                                 
    }
}
getMyValues();

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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