We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

I need a syntaxchecker and error explainer!

/*the ø stands for document.getElementById*/
function conwidth(){
var a=document.getElementsByClassName('dropdown');
var e=[];
for(i=0;i<=a.length;i++){
	if(a[i].innerHTML.length>28){
/*Safari Error (only if I bypass the setwid(j,k)command):
TypeError: 'undefined' is not an object (evaluating 'a[i].innerHTML')*/
		e.push(a[i].innerHTML);
		var j=ø(a[i].parentNode.id).parentNode.id;
		var k=200+8*(a[i].innerHTML.length-28);
		setwid(j,k);
	}
}
}
function setwid(b,c){
var dr=document.getElementsByClassName('dropdown');var st=document.getElementsByClassName('strich');var a=function(a){return ø(a.parentNode.id).parentNode.id;};
/*Safari Error: TypeError: 'undefined' is not an object (evaluating 'a.parebtNode')*/
for(i=0;i<=dr.length;i++){if(a(dr[i])==b){console.log('width');
dr[i].style.width=c+21+"px";}ø(b).style.width=c+"px";}
/*I get 14 width console.logs, but 0 strich!
but if i change their places i get 4 strich and 0 width*/
for(i=0;i<=st.length;i++){if(a(st[i])==b){console.log('strich');st[i].style.width=c+40+"px";}}
}

Could someone please explain me those errors and what I have to correct?

3
Contributors
8
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Trickist,

Main problem is in the exit conditions of the for statements, eg.:

for(i=0; i<=dr.length; i++) {...}
//should read 
for(i=0; i<dr.length; i++) {...}

The code can be simplified elsewhere, chiefly to work with elements rather than their ids.

var j = ø(a[i].parentNode.id).parentNode.id;
//will simplify to 
var j = a[i].parentNode.parentNode.id;
//and 
var j = a[i].parentNode.parentNode;
//will allow you to pass the element rather than its id to setwid(), which is more useful.

conwidth() would be improved by looping through the container (.parentNode.parentNode) elements. Looping through the dropdowns causes each container to be addressed several times over, with the nett effect that the last dropdown in each container determines its width.

You probably want the longest dropdown to determine the width of its container, requiring that (for each container) a running maximum is maintained, which feeds the statement container.style.width = maximum + "px"; (or similar).

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9

What does maximum stand for? Could you please give an example.

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

maximum would be a variable containing the running maximum calculated in the loop. Then the statement container.style.width = maximum + "px"; would be executed below the loop.

I can't rewrite conwidth() in full without knowledge of the HTML.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9
<ul class="maindropdown l" id="firdrop" style="display:none;">
    <div id="fir" class="dum">
            <li class="dropdown"></li>
            ......
    </div>
</ul>
<ul class="maindropdown l" id="secdrop" style="display:none;">
    <div id="sec" class="dum">
            <li class="dropdown"></li>
            <li class="strich"></li>
            ......
    </div>
</ul>
...
trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

That's invalid HTML. You can't have a DIV inside a UL like that.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9

I know, but it works! It's needed...

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
/*the ø stands for document.getElementById*/
function conwidth(){
var a=document.getElementsByClassName('dropdown');
var e=[];
for(i=0;i<=a.length;i++){
	if(a[i].innerHTML.length>28){
/*Safari Error (only if I bypass the setwid(j,k)command):
TypeError: 'undefined' is not an object (evaluating 'a[i].innerHTML')*/
		e.push(a[i].innerHTML);
		var j=ø(a[i].parentNode.id).parentNode.id;
		var k=200+8*(a[i].innerHTML.length-28);
		setwid(j,k);
	}
}
}
function setwid(b,c){
var dr=document.getElementsByClassName('dropdown');var st=document.getElementsByClassName('strich');var a=function(a){return ø(a.parentNode.id).parentNode.id;};
/*Safari Error: TypeError: 'undefined' is not an object (evaluating 'a.parebtNode')*/
for(i=0;i<=dr.length;i++){if(a(dr[i])==b){console.log('width');
dr[i].style.width=c+21+"px";}ø(b).style.width=c+"px";}
/*I get 14 width console.logs, but 0 strich!
but if i change their places i get 4 strich and 0 width*/
for(i=0;i<=st.length;i++){if(a(st[i])==b){console.log('strich');st[i].style.width=c+40+"px";}}
}

Could someone please explain me those errors and what I have to correct?

The javascript error is caused by your ø sign.

You need to make sure that ø symbol is a part of the language specified in the document - and javascript is depending on browser implementation possible to ignore it, therefore misinterpret it, -why not use id(...) instead of ø ?

Troy III
Master Poster
736 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

The ø is definitely not the problem, it works perfectly.I have replaced it so that it's now $.g() and there are still the same problems!

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1486 seconds using 2.74MB