Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Pull a value from inside a div then Populate a form input field with that value

    Hi all

    I have relatively easy task I need to accomplish but as per usual I have very little idea of how to do it. I have done a google search to no avail.

    Basically I need to:

    1. Pull a value from inside a div
    Code:
    <div id="number">(invoice #1017)</span>
    2. Populate a form input field with that value.

    Does this make sense? If not let me know if you need any more info.

    Thanks

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    not sure about your </span> tag there, but anyway...

    Code:
    <body>
    <div id="number">(invoice #1017)</div>
    <input type="text" id="result"></input>
    <script type="text/javascript">
    document.getElementById("result").value=document.getElementById("number").innerHTML;
    </script>
    </body>

  • Users who have thanked xelawho for this post:

    broadbeach (09-13-2011)

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Well, here's a function to pull, parse, and return the first number encountered:

    Code:
    function parseDiv(id) {
          var d = document.getElementById(id).innerHTML;
          var ints ={0:true,1:true,2:true,3:true,4:true,5:true,6:true,7:true,8:true,9:true};
          var start = 0;
          for(x in d) if(d[x] in ints) { start = x; break; }
          return parseInt(d.substring(start,d.length));
    }
    Of course, that assumes you've got javascript to work it into.
    Just pass the string value of the id of the div with the number in it.

    Hope that helps!

  • Users who have thanked Blazer2000x for this post:

    broadbeach (09-13-2011)

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    not sure about your </span> tag there, but anyway...

    Code:
    <body>
    <div id="number">(invoice #1017)</div>
    <input type="text" id="result"></input>
    <script type="text/javascript">
    document.getElementById("result").value=document.getElementById("number").innerHTML;
    </script>
    </body>
    Thanks for your help, this is exactly what I needed.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    you're welcome.

    if you want to get rid of the brackets you can do this:

    Code:
    <body>
    <div id="number">(invoice #1017)</div>
    <input type="text" id="result"></input>
    <script type="text/javascript">
    str=document.getElementById("number").innerHTML;
    document.getElementById("result").value=str.slice(1,str.length-1)
    </script>
    </body>
    or this:

    Code:
    <body>
    <div id="number">(invoice #1017)</div>
    <input type="text" id="result"></input>
    <script type="text/javascript">
    document.getElementById("result").value=document.getElementById("number").innerHTML.replace(/[(|)]/g,"");
    </script>
    </body>
    Last edited by xelawho; 09-13-2011 at 05:06 PM. Reason: continuing to answer the question long after it has been answered


  •  

    Posting Permissions

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