0

im trying to change the name of a field on the change state of a select list.

I have the following code

<script>
$('#selectintrole').change(function(){
    $('#proven_keyname').val($(this).val());
};
</script>

<select name="item_options" id="selectintrole">
<option value="20030">Universal (20030)</option>
<option value="4545456">Medium (4545456)</option>
<option value="15447">Large (15447)</option>
</select>

<input name="proven" value="1" type="checkbox" id="proven_keyname" />

It doesnt seem to be doing anything though ... when i check the generated source, nothing has changed .... am i missing something?

6
  • Because it needs to be ticked Commented Dec 21, 2010 at 0:51
  • What do you want to happen to the value? Commented Dec 21, 2010 at 0:51
  • Not to the value .... i want the name of the checkbox to change to the value of the chosen select option Commented Dec 21, 2010 at 1:03
  • Then you should call .attr('name', ...). Commented Dec 21, 2010 at 1:06
  • Is there any chance you could write the whole function ... ive added what you recommened and its still not working. Commented Dec 21, 2010 at 1:08

2 Answers 2

4

I think you need to bind your change event after the document is ready like so

     $(function () {

        $('#selectintrole').change(function () {

            $('#proven_keyname').val($(this).val());
        });

    });

If you want to change the name attribute then replace with this

$('#proven_keyname').attr('name', $(this).val());
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is running before the elements exist.

Move the script below the HTML, or wrap it in $(function() { ... }), which will run it when the page loads.

EDIT: Demo

3 Comments

@Cecil: You're also missing a ) after the handler.
Does .val($(this).val()); just change the 'value' or will it change the 'name' of the checkbox?
@Cecil: For checkboxes, it will check the matched checkboxes with those names. You may want to call .attr('value', string).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.