SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,467
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Change the value of a label dynamically

    Something I would like to do is change the value of a label. Let's say I have a textbox and onchange, I want to change the value of that label (say adding numbers together or string concatenation).

    This works fine if I use another textbox instead of a label, but I want to show Text rather than an input type. Is this possible?

    I avoid JS at all costs b/c I tend to suck with client-side programming.

    TIA
    Sara

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Labels (like most html elements) don't have value property, but they have innerHTML.

    A quick example:

    Code:
    <form>
    <input name=a onchange="update()">
    +
    <input name=b onchange="update()">
    =
    <label id=sum>0</label>
    </form>
    
    <script>
    function update() {
    	var value_a = document.forms[0].a.value;
    	var value_b = document.forms[0].b.value;
    	
    	var n_a = Number(value_a) || 0;
    	var n_b = Number(value_b) || 0;
    	
    	var label_text = n_a + n_b;
    	
    	document.
    		getElementById('sum').
    			innerHTML = label_text;
    
    }
    </script>

  3. #3
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,467
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Thank you so much. i'll give it a shot!
    Sara

  4. #4
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,467
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    works beautifully. cheers!
    Sara


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
  •