aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes While condition not working as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "While condition not working as expected" Watch "While condition not working as expected" New topic
Author

While condition not working as expected

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I am dynamically generating textfields. SO for their positioning and id,i have coded the following



1st textfield has the default value of Editbox20. Each row has 4 textfields, so the next row would have id starting from Editbox24

Even after 2nd row, i.e with Editbox24 is created, while($(idName).length!=0) { checks with idName Editbox24, return false, and everything gets out of the loop, thus overlapping 2nd row again with Editbox24,25,26,27. I found this out via browser console.
2nd last line alerts Editbox24, so this should go back to while loop condition and should again get inside the body of while. Am i right?
It looks like that while-condition is false after incrementing the counter.

what is wrong with my code ?


kunal
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Back up and explain what you are actually trying to do. I can't help but feel that there is a much simpler approach involving less code, and certainly less convolution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
i am actually trying to set the value of top position and name/id of textfields being dynamically added through this code

while loop determines the next top value and id/name after the textfield already generated
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Why not just iterate through the text fields with .each() and use the index to compute the position?

There's way too much complicated code there for what you are doing.

Also, indent your code. Unformatted code is too hard to bother reading.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Why not just iterate through the text fields with .each() and use the index to compute the position?


I have no idea how to do it. Can you please elaborate?


Okay. But still, what's wrong with the while code block?

It looks like that while-condition is false after incrementing the counter.

Even after 2nd row, i.e with Editbox24 is created, while($(idName).length!=0) { checks with idName Editbox24, return false, and everything gets out of the loop, thus overlapping 2nd row again with Editbox24,25,26,27
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Kunal Lakhani wrote:I have no idea how to do it. Can you please elaborate?

  • Create a selector that selects the text fields. Giving each of them the same class would make that easy, but you can use any selector that selects the right fields.
  • Use .each() on the resulting wrapped set.
  • One of the arguments passed to the callback is the ordinal index of the field. Use that to compute the position.

  • I question the basic premise that you need to adjust positioning after the fact at all. But you haven't given us enough information to know why you are doing all of this in the first place.

    Okay. But still, what's wrong with the while code block?

    I don't know. It's far too complicated for what it needs to do. Why waste time with it?
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Is this what are you talking about?
    Giving the textfields , class name as 'editboxes'
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    The selector is wrong.

    But I still wonder why you are doing this at all. What layout issue are you trying to address by manipulating the elements like this rather than using CSS.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    The selector is wrong.


    Can you help a bit? I am a beginner of Jquery,CSS
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Class selectors start with a period character. The selector for class xyz is ".xyz"
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    I have tried the following code



    But this code doesn't works
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Please read ItDoesntWorkIsUseless. Thanks.

    Open the JavaScript debugger in your browser, set a breakpoint at the first line of the callback, and step through the function to see at what point it's not doing what you expect it to be doing.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    What i found is that while($(idName).length!=0) { has incorrect condition. idName.length checks for the length for idName, which cannot be 0 ever. I tried for "alert($(idName).length)" where idName = any id that does not exists, i tried with "Editbox24xxxxx" and it gave me the length of the name 'Editbox24xxxxx', i.e 14

    I saw the documentation. It appears that $(...).length returns the number of elements found. With proper id it would be always 1 if found, 0 otherwise.
    But really confused about the alert with 'Editbox24xxxxx' giving output as 14
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Kunal Lakhani wrote:I saw the documentation. It appears that $(...).length returns the number of elements found. With proper id it would be always 1 if found, 0 otherwise.

    Yes. That is correct.

    If you are seeing otherwise, please post an SSCCE that demonstrates the issue.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    That means "while($(idName).length!=0) { " can't check whether the textfield with such id exists or not?

    I posted the alert statement with an awkward id.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Yes, that means that it can. Assuming that variable idName contains the correct selector.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    So, what Jquery method should i use to check whether a particular textfield id exists or not?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    I already said that the .length property can be used. If you're not getting the expected results, it most likely means that your selector is wrong.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622


    Assuming there is no id with '#Editbox24xxx', then while loop body won't be invoked?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Why a while loop in the first place?

    Seems silly to use a while rather than an if for a condition that will always be 1 or 0.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    I need to check for the value of Editbox22,Editbox26...on the difference of 4 till they exists
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: While condition not working as expected