Solved

Create New Div's with styles inside the Div

Posted on 2011-09-15
14
286 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
  • 8
  • 6
14 Comments
 
LVL 75

Expert Comment

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

Author Comment

by:Gurpreet Singh Randhawa
ID: 36544309
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
ID: 36545841
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36546231
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36546239
i am getting an error in second approach
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36547255
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36547634
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36547752
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36547764
ok i will try and get back to you
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36551608
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36551610
please guide
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36553197
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36553329
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
ID: 36553661
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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