The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> javascript : Client-side chat
belowHeights00
post Apr 18 2014, 03:10 PM
Post #1


Member
***

Group: Members
Posts: 67
Joined: 4-February 14
Member No.: 20,319



Here is my js code: function postQuestion() {
var questionOne = $('.question:nth-child(1)');
var questionTwo = $('.question:nth-child(2)');
var questionThree = $('.question:nth-child(3)');
var questionFour = $('.question:nth-child(4)');
var questionFive = $('.question:nth-child(5)');
var question = $('#input').val();
if ($('.question:nth-child(1)').html() == "")
{
questionOne.append(question);
}
else if ($('.question:nth-child(1)').html() != "")
{
questionTwo.append(questionOne.html());
questionOne.empty();
questionOne.append(question);
}
else if ($('.question:nth-child(2)').html() != "")
{
questionThree.append(questionTwo.html());
questionTwo.empty();
questionTwo.append(questionOne.html());
questionOne.empty();
questionOne.append(question);
}
else if ($('.question:nth-child(3)').html() != "")
{
questionFour.append(questionThree.html());
questionThree.empty();
questionThree.append(questionTwo.html());
questionTwo.empty();
questionTwo.append(questionOne.html());
questionOne.empty();
questionOne.append(question);
}
else if ($('.question:nth-child(4)').html() != "")
{
questionFive.append(questionFour.html());
questionFour.empty();
questionFour.append(questionThree.html());
questionThree.empty();
questionThree.append(questionTwo.html());
questionTwo.empty();
questionTwo.append(questionOne.html());
questionOne.empty();
questionOne.append(question);
}

}

Aside from using a more condensed way to code that...Why wouldn't it be working? If the 1st child is not equal to "" (null), it goes down to the 2nd child and the 1st child's html becomes question --- but if the 2nd child is not equal to "" (null), the 1st child IS replaced by question, and the 2nd child is replaced by the 1st child's html, but the 2nd child keep the original html AND keep the original html (which is supposed to go down a level).

The .question classes are just simply div's btw.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
belowHeights00
post Apr 18 2014, 08:51 PM
Post #2


Member
***

Group: Members
Posts: 67
Joined: 4-February 14
Member No.: 20,319



haha if I had the variables already made why didn't I use them? Anyway, still can figure out why it doesn't work -- need another eye I think.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 19 2014, 06:47 AM
Post #3


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



That looks more like jQuery than pure javascript so I can't help with that. Have you tried e.g. alertboxes to check the state in each ELESE/IF statement?

BTW I think "" is an empty value, not null. Alas most javascript pages that discuss this seem to claim everybody else is wrong, so you may want to research this a bit.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
belowHeights00
post Apr 19 2014, 07:36 AM
Post #4


Member
***

Group: Members
Posts: 67
Joined: 4-February 14
Member No.: 20,319



Thanks for the reply Christian. yes, it is more jquery I suppose than javascript (I think I can convert it to straight javascript though). I said null just to express that it is an empty value..I wonder if "" won't work for append?

Wait, no because the first 2 expressions work fine..then after child 3 -- it doesn't wanna go to child 4.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 19 2014, 08:19 AM
Post #5


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



CODE
if ($('.question:nth-child(1)').html() == "") // child(1) is empty

else if ($('.question:nth-child(1)').html() != "") // child(1) is not empty

One of the two conditions above should always be true, so how could the script ever move on to the subsequent ELSE IF statements? For the following to be be evaluated:

CODE
else if ($('.question:nth-child(2)').html() != "")

child(1) must neither be empty nor non-empty.

Perhaps you could nest the statements more.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 12:20 PM