www.webdeveloper.com
Results 1 to 4 of 4

Thread: Getting value from span using javascript

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Getting value from span using javascript

    Hello, I am trying to get the value(34.88) of a span in a CSS ussing javascript. I tried using getElementById with and without innerHtml but all that returns is null. Below is an example using the getElementById.

    var s1 = document.getElementById('product'');

    alert(s1);

    I also tried the example with innerHtml and got nothing returned.

    var s1 = document.getElementById('yfs_l10_palm').innerHtml;

    alert(s2);


    Thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    184
    It must be the ordering of elements/scripts keeping the value null.

    This works because the <span> element is loaded before the javascript:
    Code:
    <html>
    <body>
    
    <span id='s1'>34.88</span>
    
    <script type="text/javascript">
    var s1 = document.getElementById('s1').innerHTML;
    alert(s1);
    </script>
    
    </body>
    </html>

  3. #3
    Join Date
    Jun 2010
    Posts
    2

    Getting value from span using javascript

    Quote Originally Posted by WestWeb View Post
    It must be the ordering of elements/scripts keeping the value null.

    This works because the <span> element is loaded before the javascript:
    Code:
    <html>
    <body>
    
    <span id='s1'>34.88</span>
    
    <script type="text/javascript">
    var s1 = document.getElementById('s1').innerHTML;
    alert(s1);
    </script>
    
    </body>
    </html>

    Thanks for the info. How can I load the span before the javascript if the page
    that has the span is a new page which is the result of a search page?. The new page has the span in it.

    Thanks

  4. #4
    Join Date
    Jul 2007
    Posts
    386
    Use:

    Code:
    window.onload = function(){
        // any code you want executed after the page loads
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles