SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need a little help with a function

    I have this function
    Code JavaScript:
    <script type="text/javascript">
    function alertValue()
    {
    var x=document.getElementById("processor").selectedIndex;
    var y=(document.getElementsByTagName("option")[x].value.split(', '));
    alert(y[1]);
    }
    </script>

    Rather than open an alert box I would like it to populate an input box instead.

    Code HTML4Strict:
    <select name="processor" id="processor" onChange="alertValue()">
        <option>&hellip;Choose</option>
        <option value="1, 10.00">foo</option>
        <option value="2, 20.00">bar</option>
       <!-- first value is UID of item, second value is price -->
        </select>
        <input type="text" name="processor_price" id="processor_price" class="price" value="" readonly="readonly">

    I would like the price of item in select id="processor" to appear in input id="processor_price"

    I'm guessing this probably would be done with innerHTML, but I'm a bit of a javascript noob.

    Can anyone help?
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script type="text/javascript">
    function alertValue()
    {
    var x=document.getElementById("processor").selectedIndex;
    var y=(document.getElementsByTagName("option")[x].value.split(', '));
    document.getElementById("processor_price").value = y[1];
    }

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,161
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    I'm not sure where "innerHTML" stands support-wise ATM. A bit more code to write, but I think you would be better of using document.createElement(), appendChild(), setAttribute(), etc.

  4. #4
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cont911 View Post
    <script type="text/javascript">
    function alertValue()
    {
    var x=document.getElementById("processor").selectedIndex;
    var y=(document.getElementsByTagName("option")[x].value.split(', '));
    document.getElementById("processor_price").value = y[1];
    }
    That works fine, how would I make this function more generic?

    I have several select boxes that I would like to use this function on and they all follow the same naming conventions.

    foo
    foo_price
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Here's a more generic version of the function that has also been refactored so that it's a lot clearer as to what's happening.

    Code javascript:
    function alertValue(id) {
        var select = document.getElementById(id);
        var option = select.options[select.selectedIndex];
        var price = option.value.split(', ')[1];
        document.getElementById(id + '_price').value = price;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some of the select boxes have an associated quantity box, how would I factor this in?

    foo
    foo_price
    foo_quantity
    Last edited by blain; Nov 14, 2008 at 04:08.
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.


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
  •