• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

While condition not working as expected

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is this what are you talking about?
    Giving the textfields , class name as 'editboxes'
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    The selector is wrong.



    Can you help a bit? I am a beginner of Jquery,CSS
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Class selectors start with a period character. The selector for class xyz is ".xyz"
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have tried the following code



    But this code doesn't works
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, that means that it can. Assuming that variable idName contains the correct selector.
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So, what Jquery method should i use to check whether a particular textfield id exists or not?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Assuming there is no id with '#Editbox24xxx', then while loop body won't be invoked?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 622
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I need to check for the value of Editbox22,Editbox26...on the difference of 4 till they exists
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic