SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementByID - string to number conversion

    I have been trying to implement this for a while, but for some reason I get errors both using the parseInt() or not.

    PHP Code:
        // this is the total number of fields visable, and highest index to have to loop through
        
    var element getElement('numFields');
        var 
    highest getElement('highField');
        
        
    // this is the incrementing that happens
        
    element.value = (element.value -1)+ 2;
        if (
    element.value >= highest.value) { highest.value element.value; } 
    The idea is that 'numFields' and 'highField' are hidden form elements that allow me to parse a dynamically created form of n elements. For example (stupid but pointed) : if you wanted to make 3 simultaneous quick posts to respond to my initial post, you could just dynamically add more and more fields... but then the PHP/Perl back-end has to be able to know how many you have...

    Hope somebody understands what Im saying...

    Anyway, the problem is that if I use the parse methods, I end up with NaN no matter what... and if I use the way that I provided, the last if statement fails to update the highest value after it goes into double digits... ie I am limited to 9... even though element.value keeps climbing AND I keep adding fields successfully too...

  2. #2
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should be using "getElementById" instead of "getElement". Also, make sure your hidden fields have an unique "id" attribute associated with them.
    Patrick Smith
    PHP Programmer

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    woops, my bad, I probably should have included this tidbit in the original post

    PHP Code:
    function getElement(id) {
      if (
    document.getElementById) {
        
    // this is the way the standards work
        
    return document.getElementById(id);
      } else if (
    document.all) {
        
    // this is the way old msie versions work
        
    return document.all[id];
      } else if (
    document.layers) {
        
    // this is the way nn4 works
        
    return document.layers[id];
      } else {
        
    // there is no such layer with that id
        
    return false;
      }

    also, the two fields in question are unique to the form, not to each dynamic element, and they are IDed as 'numFields' & 'highField'

  4. #4
    code ninja hamstu's Avatar
    Join Date
    Nov 2002
    Location
    Montréal, Canada
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the relevant HTML as well?

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will this work?
    Code:
        var element = getElement('numFields'); 
        var highest = getElement('highField'); 
         
        var elementValue = parseInt(element.value, 10);
        ++elementValue;
        if (elementValue >= parseInt(highest.value, 10)) { highest.value = element.value; }
    You must make sure to initialize both hidden fields with a numeric value, of course.
    HTML Code:
    <input type="hidden" id="numFields" value="0">
    <input type="hidden" id="highField" value="0">
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    Will this work?
    Code:
        var element = getElement('numFields'); 
        var highest = getElement('highField'); 
         
        var elementValue = parseInt(element.value, 10);
        ++elementValue;
        if (elementValue >= parseInt(highest.value, 10)) { highest.value = element.value; }
    You must make sure to initialize both hidden fields with a numeric value, of course.
    HTML Code:
    <input type="hidden" id="numFields" value="0">
    <input type="hidden" id="highField" value="0">
    I changed the code, cause you forgot to increment the element.value too, and it worked...

    Code:
    	// this is the total number of fields visable, and highest index to have to loop through
    	var element = getElement('numFields');
    	var highest = getElement('highField');
    	
    	var elementValue = parseInt(element.value, 10);
        ++elementValue;
    	++element.value;
        if (elementValue >= parseInt(highest.value, 10)) { highest.value = element.value; }
    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •