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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    78
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Change input value based on select value?

    Hello, I am trying to change the value of #name and #category inputs based on the value of select. A friend gave me the code below, but I can't get it to work. Thanks so much for your help

    Code:
         
    <script type="text/javascript">  
    $('#Gift_Type').change(function(){
    if ($(this).val() == 'Monthly'){
    $('#name').val('Partnership')
    $('#category').val('Partnership')
    } else {
    $('#name').val('Donation')
    $('#category').val('Donations')
    }
    })
    </script>
    Code:
    <SELECT NAME="sub_frequency" ID="Gift_Type" onchange="OnSelectionChanged (this)">
    <OPTION VALUE="1m">Monthly</OPTION>
    <OPTION VALUE="">One Time</OPTION>
    </SELECT>
    Code:
    <input type="text" id="name" name="name" value="Partnership" />
    <input type="text" id="category" name="category" value="Partnership" />

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    1. Please remove onchange="OnSelectionChanged (this)" from the SELECT
    2. Please wrap your remaining jQuery code in
    Code:
    $(document).ready(function() {
       // your code here
    });
    to make sure that the element exists before you try to attach an event handler to it

    3. The value of the first option is "1m" and not "Monthly"

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    78
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you so much for your help, but I made suggested changes, but it still doesnt work. I have included the rest of my javascript (may be what is tripping it up) I need to keep the "OnSelectedChange" Thank you so much for your help!

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    $('#1y').hide();
    $('#One_Time').hide();
    $("#Gift_Type").change(function(){
    $("#" + this.value).show().siblings().hide();
    });
    $("#Gift_Type").change();
    });
    </script>
    
    <script type="text/javascript">
    $('#Gift_Type').change(function(){
    var $sel = $(this);
    if($sel.val() == ''){
    $sel.attr('name','');
    } else {
    $sel.attr('name','sub_frequency');
    }
    });
    </script>
    
    <script type="text/javascript">  
    $(document).ready(function() {
    $('#Gift_Type').change(function(){
    if ($(this).val() == '1m'){
    $('#name').val('Partnership')
    $('#category').val('Partnership')
    } else {
    $('#name').val('Donation')
    $('#category').val('Donations')
    }
    })
    });
    </script>
    Code:
    <SELECT NAME="sub_frequency" ID="Gift_Type" onchange="OnSelectionChanged (this)">
    <OPTION VALUE="1m">Monthly</OPTION>
    <OPTION VALUE="">One Time</OPTION>
    </SELECT>
    Code:
    <input type="text" id="name" name="name" value="Partnership" />
    <input type="text" id="category" name="category" value="Partnership" />

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Sorry, but what do you need OnSelectionChanged() for? It is nowhere in your code

    And you still have parts of the code not wrapped in $(document).ready(). You should definitely start to debug your code.


  •  

    Posting Permissions

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