Solved

Create New Div's with styles inside the Div

Posted on 2011-09-15
14
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

717 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