Use Id as Table name in query

Should work fine. Maybe IE8 wants a space between else and the curly brace?

Also, please beware that your code will be executed as


[COLOR="#FF0000"]if () { 

}[/COLOR]
[COLOR="#0000FF"]if () {

}
else {

}[/COLOR]

i.e., the red if is completely separate from the blue if and else (so if the red if matches, the blue if will not match, but the blue else will!).

If you want either A, or B, or otherwise … you should use


if () {
  // do something
} [B]else if[/B] {
  // do something else
} else {
  // none of the above matched
} 

:slight_smile:

OK. I got the first if to run by itself but couldn’t get two ifs into the function even when I added the else statement so I am guessing the ‘for in loop’ requires a different syntax to add if and else statements

IF, IF AND ELSE


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
for (i = 0; i < sidebarli.length; i++) {  //for in loop 
if (sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'prod') {  
// excludes class="item disabled" runs when it is the only if statement?
//parentNode.className excludes ul class="site" perfect
sidebarli[i].onclick = function() {   
alert(this.id+': Is sending your request.');  // this actually gets the right id
var p=this.id; 
getproduct(p);//works on it's own
}
if (sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'site') {  
// excludes class="item disabled" 
//runs when it is the only if statement??
sidebarli[i].onclick = function() {   
var p=this.id;
f1.innerHTML='<object id='+P+'html class=htmlpage type=text/html data='+p+'.html></object>';
}
else{//draws syntax error here
alert(this.id); 
//should never occur  
}   
}

I’m prity sure the for in loop reguires a different if else syntax but maybe it dosen’t allow two ifs or an else statement at all? Thanks for any direction in advance.

Nope, if {} else {} are the same always, no matter where you put them (inside a for or otherwise …). Have you tried a space after the else ?

You have some mismatched { } in there - you can’t have an else attached to sidebarli[i].onclick = function() {, it needs to be attached to an if statement.

The following should help you figure it out as the indentation is based on which} match to which {

var sidebarli = document.getElementById('sidebar').getElementsByTagName('li'); 
for (i = 0; i < sidebarli.length; i++) {
   //for in loop 
   if (sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'prod') {
      // excludes class="item disabled" 
      //parentNode.className excludes ul class="site" perfect
      sidebarli[i].onclick = function() {
         alert(this.id + ': Is sending your request.'); 
         // this actually gets the right id
         var p = this.id; 
         getproduct(p); 
         //works on it's own
         }
      if (sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'site') {
         // excludes class="item disabled" 
         //runs when it is the only if statement??
         sidebarli[i].onclick = function() {
            var p = this.id; 
            f1.innerHTML = '<object id=' + P + 'html class=htmlpage type=text/html data=' + p + '.html></object>'; 
            }
         else {
            //draws syntax error here
            alert(this.id); 
            //should never occur 
            }

felgall Thanks for the input to. I’m just glad I’m not barking up the wrong tree. I can’t see how the else statement is attached to the i onclick on it’s own I just asumed it was automatically attached so that any other li elements clicked that did not meet the conditions of the previous if statemets the else would be fired. I’m not sure your telling me the else should be ‘else if (’ or just ‘else_{’
LIKE:


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
for (i = 0; i < sidebarli.length; i++) {   
if (sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'prod') { 
sidebarli[i].onclick = function() {   
alert(this.id+': Is sending your request.');
var p=this.id; 
getproduct(p);
}
if (sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'site') {
sidebarli[i].onclick = function() {   
var p=this.id;
f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
}
else { //or 'else if(sidebarli[i].className == 'item' && sidebarli[i].parentNode.className == 'search') {
alert(this.id+': Requiers 5 digit input.');   
}   
}

Actually they both draw a syntax error so I have to go back to felgalls post.

Sorry felgall I can’t see which { matches which } even though I wondered why people staggerd their script I don’t see how it points out which bracket matches another. I got to call it a day. Hopefully I’ll get it tomorrow. Thanks again for everyones help. On the bounce.

@ScallioXTX - thanks for the indexOf trick. Totally forgot about that and went a longer PHP route.

ScallioXTX
Thanks for that and the indexof syntax. The function in the second code div syntax is what I’m looking for. IF, ELSE IF, ELSE. felgall said that there is a mismatch of the { } brackets in the way I wrote the function and reminded me why the script Gods stagger their Functions so that they can figure out which match each other. I myself never use that method and align all my text to the left so as to avoid scrolling to see the whole function.
centered affect actually got my original question answered. How would I pass the Id of an element clicked as the Table name in a sql query. Turns out “SELECT/*FROM_”.$q) was correct and I swear I searched the web for two weeks to find that tastey tid bit and never found it. Thank you Swami.

Proposed Function


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
for(i=0;i<sidebarli.length;i++){   
if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
sidebarli[i].onclick=function(){   
alert(this.id+': Is sending your request.');
var p=this.id; // centered effect @sitepoint forums javascript
getproduct(p);
}
else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){
sidebarli[i].onclick=function(){   
var p=this.id;
f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
}
else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='search'){
alert(this.id+': Requiers 5 digit input.');   
} 

So there is a mismatch of the curly brackets { } that I can’t see. IE8 doesn’t requier a space between the if_{ or other statements as far as I know. I try to write all of my functions without additional white space. As far as I can see my function copies the syntax of ScallioXTX previous post. It maybe a Jscript syntax thing?

ScallioXTX
I guess I’m asking for the full syntax of functions within IF, ELSE IF and ELSE
SOMETHING LIKE THIS


if () {
  // do something
} else if (//brackets not excepted?) { //throws syntax error
  // do something else
} else (//brackets not excepted?) {
  // none of the above matched
}

Function


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
for(i=0;i<sidebarli.length;i++){   
if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
sidebarli[i].onclick=function(){   
alert(this.id+': Is sending your request.');
var p=this.id; // centered effect @sitepoint forums javascript
getproduct(p);
}
else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){//syntax error
sidebarli[i].onclick=function(){   
var p=this.id;
f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
}
else(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='search'){
alert(this.id+': Requiers 5 digit input.');   
} 

Eep, I forgot the brackets in the else if.

Suppose we want to do something based on the value of a variable a, the full syntax is


if (a==2) {
   // do something with a
} else if (a==3) {
  // do something else with a
} else {
  // a is not 2 and it's also not 3
  // do yet something else with a here
}

But as I pointed out in my earlier post you only need one if/else pair, not if/else if/else; either an item has the class “disabled” (in which case you don’t perform the AJAX call) or it does (in which case you do perform the AJAX call).

In your if/else if/else you’re checking for the classname of the parent, but that’s completely irrelevant for your problem (think about it).

ScallioXTX
As al the li elements in all the ul elements in #sidebar div may be asigned the classes ‘item’, ‘item disabled’ or ‘item visited’ the parentNode is important because there ar three types of httprequests being made and I am trying to accomodate all three request in one function

  1. uls 0 to 3 in sidebar div httprequest product table
  2. ul 4 in sidebar div httprequest writes object to f1 div
  3. ul 5 in sidebar div httprequest searches the whole database by id =code and returns the table that code number is found in

It is not the indexof class that I am trying to apply. I need to assign any of the 3 class to all of the li elements in sidebar div and therefor can only exclude the anything but ‘item’ and the parentNode ul to apply a seperate httprequest. Thanks for your patience again.

MSDN LIBRARYvar z = 3;
Both ambigious and cryptic and though it claims to be EMCA complient Windows Script 3.* (Jscript) is still slightly different and has no ‘else if’ statement but this is the syntax they recomend for ‘if…else’

 
if (x == 5) {
    if (y == 6)
        z = 17;
}
else
    z = 20;

MY FUNCTION ENDEAVOUR JSCRIPT 3.*


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
for(i=0;i<sidebarli.length;i++){   
if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
sidebarli[i].onclick=function(){   
alert(this.id+': Is sending your request.');
var p=this.id; // centered effect @sitepoint forums javascript
getproduct(p);
if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){
sidebarli[i].onclick=function(){   
var p=this.id;
f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
}
else{ // Throws syntax error here
alert(this.id+': Requiers 5 digit input.');   
} 

Why not do a switch statement based on post #29:

Untested…


var sidebarli = document.getElementById('sidebar').getElementsByTagName('li'),
	i;   
for(i = 0; i < sidebarli.length; i++) {   
	if(sidebarli[i].className == 'item') {
		switch( sidebarli[i].parentNode.className ) {
			case 'prod' : 
				sidebarli[i].onclick = function() {};
			break;
			case 'site' :
				sidebarli[i].onclick = function() {};					
			break;
			case 'search' :
				sidebarli[i].onclick = function() {};
			break;
		}		
	}
}

Let me try and break this down for you. This is what’s happening now –


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
[COLOR="#FF8C00"]for(i=0;i<sidebarli.length;i++) { [/COLOR]  
  [COLOR="#800080"]if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod') {[/COLOR]   
    [COLOR="#006400"]sidebarli[i].onclick=function() { [/COLOR]   
      alert(this.id+': Is sending your request.');
      var p=this.id; // centered effect @sitepoint forums javascript
      getproduct(p);
      [COLOR="#FF0000"]if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site') {[/COLOR]
        [COLOR="#0000CD"]sidebarli[i].onclick=function() {[/COLOR]   
          var p=this.id; 
          f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
        [COLOR="#0000CD"]}
        else{[/COLOR] // Throws syntax error here
          alert(this.id+': Requiers 5 digit input.');   
        [COLOR="#0000FF"]}[/COLOR] 

This is what should happen –


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
[COLOR="#FF8C00"]for(i=0;i<sidebarli.length;i++) {[/COLOR]   
  [COLOR="#800080"]if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod') {[/COLOR]   
    [COLOR="#008000"]sidebarli[i].onclick=function() {  [/COLOR]  
      alert(this.id+': Is sending your request.');
      var p=this.id; // centered effect @sitepoint forums javascript
      getproduct(p);
      [COLOR="#FF0000"]if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site') {[/COLOR]
        [COLOR="#0000CD"]sidebarli[i].onclick=function() {[/COLOR]   
          var p=this.id; 
          f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
        [COLOR="#0000CD"]}[/COLOR]
      [COLOR="#FF0000"]} else {[/COLOR]
        alert(this.id+': Requiers 5 digit input.');
      [COLOR="#FF0000"]}[/COLOR]   
    [COLOR="#008000"]}[/COLOR]
  [COLOR="#800080"]}[/COLOR]
[COLOR="#FF8C00"]}[/COLOR]

See the difference?

Basically, if you type a { , you indent the next line (by a tab, or spaces, or whatever). Then if you want to close that block you go to the same position on further line and put a } there.
It would be really helpful for you if you tried and understand this and implement it. It really makes coding a lot easier. In fact, your code with everything crammed to the left makes me a little bit dizzy…

Quick thanks to Every One. felgall SWAMI BAGWAN. Your post as usual was over CRYPTIC and maybe the teaching style of erra I personally shall not miss. It would have been a lot more efficient to say ‘TRY CLOSSING YOUR FUNCTIONS IN THE IF OR ELSE STATEMENTS.’ That was the issue and the following script works to prove it.
Also ScallioXTX, as I mentioned previously to avoid scrolling to see the complete code I refuse to stagger the writing of functions. To see the script as it’s final writing maybe imposible as it might well be one line by time it is crunched for production and all students would be better served viewing code line by line left aligned text as the staggering/indent of lines by braces did not point out to me the fact that several closing braces where not there. Once again that is the teaching style of an erra I personally shall not miss. It is far quicker to crunch your code when you learn to write it without extra white space and that method of instruction was primarly a visual aid which we have overcome with magnification ability witch exaserbates the stagering technique problem. People need to know how to write code exactly so that code needs the littlest amount of manipulation to get it running. I am a ‘No Extra White Space’ type of guy particularily when I am on my P’S and Q’S. At least every other month for an hour or two:confused:

A SCRIPT THAT SURVES MY PURPOSE TO FOLLOW

Thanks agin to SITEPOINT and all of the persons that contributed to this post.

The SKRIPT THAT WORKS!


var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
for(i=0;i<sidebarli.length;i++){   
if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
sidebarli[i].onclick=function(){   
alert(this.id+': Is sending your request.');
var p=this.id; 
getproduct(p)}; //closed nested function with '};'
}
else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){
sidebarli[i].onclick=function(){   
var p=this.id;
f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>'}; 
 //closed nested function with '};'
}
else if(sidebarli[i].id=='searchlist' && sidebarli[i].parentNode.className=='bwi'){
sidebarli[i].onclick=function(){
alert(this.id+': Requiers 5 digit input.')}; //closed nested function with '};'
}} 

Any further comments greatly appreciated.
Hands up, all the people who agree there is ‘To Much Extra ‘White Space’ in the House, Senate and Congress.’
Children, please remember to close your functions properly, particularly if they are contained within any statements.

Yeah centered effect a switch statement would be the modern way of doing it and probably the correct way to change the ‘onclick’ method in the first two cases to ‘onchange’ for the last li element case which contains a text type input that querys the entire database. How to apply case3:if li element childNode 0 onchange is ’ ’ would be the trick. I’ll work on something and postback. This post is getting to long? Will try latter with switch statement to alter event in third case.

Best regards, Heinz Stapff

Actually (and I hoping someone corrects me if I am wrong), that my first proposed method was the more modern way of doing this. As you can see the if/else if/else can get long, unreadable and unmaintainable - especially when you are not indenting your code (and adding spaces too!).

centered effect You are probably correct and the EMCA Script versions will hold the record of the chronology of switch/case and if/else syntax. Either way you can’t knock the old school guys. They had to do what we try to do with 286 cpus. I was thinking of adding functionality with switch/case syntax like changing the logo background image and playing a sound when the li element was clicked but I’ve done that with if/else and lack of experiance with switch/case has stopped the endeavour with switch/case. I’ll write it as soon as I get the chance. I guess we are all creatures of habit and are not comfortable stepping out of those habits. Might just be the deadline though mine was 9 years ago! The number one advantage of switch/case would be clarity of process in the function as written. I guess we choose to live in a more ambiguous world anyway.