/    Sign up×
Community /Pin to ProfileBookmark

Problem Nesting elements with same class name

When the code below is loaded into a browser, the DOM gets built incorrectly. Specifically, the final }); text node does not appear inside the code element it belongs to, but rather as a sibling within the p element. I can fix it by simply changing the class name of the innermost code elements from “new” to “new2”, or by removing them altogether. This indicates to me that there must be some kind of limit on nesting tags of the same class, but I can`t find a reference to this phenomenon anywhere else.

Can anyone out there shed any light on this?

[CODE]<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<style type=”text/css”>
code.first {
margin-left:20px;
margin-top:10pt;
margin-bottom:10pt;
font-size: inherit;
display:block;
}
code.new {
margin-left:30px;
display:block;
}
</style>
</head>
<body>
<p>The JavaScript code below is from the |ajaxTime.html| example. Take a moment to read it over and try to understand it.
<code class=’first’>
$(document).ready( function() {
<code class=’new’>
var launchAjax = function() {
<code class=’new’>
$.get(
<code class=’new’>
“servers/timeServer.php”,
<br>{
<code class=”new”>
timezone: $(“[name=timezone]”).val(),
<br>request: $(“[name=type]”).val(),
<br>delay: $(“[name=delay]”).val(),
</code>
},
<br>function(data, textStatus, jqXHR) {
<code class=”new”>
$(“#result”).html(data);
</code>
};
</code>
)};
</code>
$(“#ajaxButton”).click(launchAjax);
</code>
});
</code>
</p>
</body>
</html>[/CODE]

to post a comment
HTML

4 Comments(s)

Copy linkTweet thisAlerts:
@Nicholas_DiazJun 19.2013 — It appears you are setting your divs and ending them all at the end of the page so they are divs with in divs. if one of them is supersceding a parent div class this could be because its choosing to default to one loaded to the DOM first. go in to the class which is not getting rendered properly and add !important to the specific attribute of the div that is not functioning properly and see if that helps. something like this

p { color: #ff0000 !important; }

Sorry if that does not help maybe I am misunderstanding your problem.

Also if the point of these divs is to render each link in to the same div could you not close each section off and start the next one so they are not all children of the first?
Copy linkTweet thisAlerts:
@SamScottauthorJun 19.2013 — Hi Nicholas,

Thanks for the response, but I don't think you are understanding the problem. The problem is not that style properties are not being applied. The problem is that the DOM tree is not being created in the browser properly. The final text node with contents "});" should be a child of the first <code> element, but it is not. Rather it appears as a child of the <p> element and a sibling of the <code> element. This can be fixed simply by changing the class of the innermost <code> elements to something other than "new". This happens on both firefox and chrome (haven't checked other browsers).

This does not happen with <div> elements, which behave as you would expect.

This behavior seems odd to me, and wrong, so I'm asking for any info that would shed some light on why this is happening.

Thanks,

Sam.

It appears you are setting your divs and ending them all at the end of the page so they are divs with in divs. if one of them is supersceding a parent div class this could be because its choosing to default to one loaded to the DOM first. go in to the class which is not getting rendered properly and add !important to the specific attribute of the div that is not functioning properly and see if that helps. something like this

p { color: #ff0000 !important; }

Sorry if that does not help maybe I am misunderstanding your problem.

Also if the point of these divs is to render each link in to the same div could you not close each section off and start the next one so they are not all children of the first?[/QUOTE]
Copy linkTweet thisAlerts:
@SamScottauthorJun 19.2013 — Update: Opera, Firefox, Internet Explorer, and Chrome get it wrong. Safari gets it right. To see the difference, try the code below. The alert box says "undefined" on all browsers except Safary, which reports "});", which I believe is the correct answer.

[CODE]<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
code.first {
margin-left:20px;
margin-top:10pt;
margin-bottom:10pt;
font-size: inherit;
display:block;
}
code.new {
margin-left:30px;
display:block;
}
</style>
</head>
<body>
<p>The JavaScript code below is from the |ajaxTime.html| example. Take a moment to read it over and try to understand it.
<code class='first'>
$(document).ready( function() {
<code class='new'>
var launchAjax = function() {
<code class='new'>
$.get(
<code class='new'>
"servers/timeServer.php",
<br>{
<code class='new'>
timezone: $("[name=timezone]").val(),
<br>request: $("[name=type]").val(),
<br>delay: $("[name=delay]").val(),
</code>
},
<br>function(data, textStatus, jqXHR) {
<code class='new'>
$("#result").html(data);
</code>
};
</code>
)};
</code>
$("#ajaxButton").click(launchAjax);
</code>
});
</code>
</p>
<script type="text/javascript">
alert(document.querySelector(".first").lastChild.data);
</script>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@Nicholas_DiazJun 19.2013 — ahhh I am sorry for misunderstanding. and the worst thing is I have built two sites using ajax and I was very displeased with my self on both of them. I gather since you have found a fix for this problem you just want to understand why its behaving the way it is. You have my curiosity as well. I hope someone comes along with an answer because this is my weakest area and I really need to learn more about it.
×

Success!

Help @SamScott spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...