Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 966983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:14:42+00:00 2026-05-16T02:14:42+00:00

Is it possible to change the name value of an input element? Here is

  • 0

Is it possible to change the name value of an input element?

Here is my code but i can set anything but the name.

$('#country').change(function(){
    var $country = $(this);
    var $state = $('#state');
    var $county = $("#county");
    if($country.val() != 226){
        $state.attr('name', '');
        $state.closest('p').hide();
        $county.attr('name', 'user[state]');
        $county.closest('p').show();
    }else{          
        $state.attr('name', 'user[state]');
        $state.closest('p').show();
        $county.attr('name', '');
        $county.closest('p').hide();            

    }
});

Any ideas why setting a name attr does not work ?

Hope you can advise

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:14:42+00:00Added an answer on May 16, 2026 at 2:14 am

    I’m taking a shot in the dark here, it seems like you want to prevent these elements from submitting to the server, if that’s the case instead of trying to change the name attribute, you can set the disabled attribute, like this:

    $('#country').change(function() {
        var $country = $(this), $state = $('#state'), $county = $("#county");
        if($country.val() != 226) {
            $state.attr('disabled', true).closest('p').hide();
            $county.attr('disabled', false).closest('p').show();
        } else {
            $state.attr('disabled', false).closest('p').show();
            $county.attr('disabled', true).closest('p').hide();
        }
    });
    

    When a form element has the disabled attribute, it can’t be successful, which means it won’t be included when you submit it to the server. So in the above example just give both inputs name="user[state]" from the start, and this will exclude the one you don’t want from the POST (or GET) that your <form> does.

    It doesn’t answer the question directly, I realize this, but it’s another (I think) simpler way to avoid the problem 🙂 You can shorten this down further by using .toggle(bool) but I think it destroys the example, so I left it uncompressed above, the short version would look like this:

    $('#country').change(function() {
      var is226 = $(this).val() == 226;
      $('#state').attr('disabled', !is226).closest('p').toggle(is226);
      $("#county").attr('disabled', is226).closest('p').toggle(!is226);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.