1

http://jsfiddle.net/uz79M/ Here's my pretty simple code, can anyone tell me why the hell it wont start working xD I'm missing something silly ain't I? Why does the damn value remain on ="test"?

Thanks for reading!

(P.S.: Did I get my tags right or should I delete/replace some of them?)

3
  • jsfiddle.net/uz79M/1 i have made the change and share it . val will work Commented Sep 5, 2013 at 10:33
  • wow the reaction time on this by you guys was amazing xD I'll give everyone +1 once I get to the 15 rep ^_^ Thank you so much everyone! Commented Sep 5, 2013 at 10:37
  • I'm at 17 now so did as promised :) Commented Sep 5, 2013 at 12:13

15 Answers 15

15

use .val() to change the value of an input element

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);

Ex: (I've done some more clean up in the code)

jQuery(function ($) {
    $("#AttachmentTypeSelect").change(function () {
        var selectvalue = $(this).val();
        if (selectvalue !== "select") {
            $('input[name="prop_sc_AttachmentType"]').val(selectvalue);
        }
    });
});

Demo: Fiddle

Also the use of .attr() was wrong, it should be

$(element).attr(attribute, value)

ex

$(element).attr('tab-index', 1)
Sign up to request clarification or add additional context in comments.

Comments

5

Your code is not correct, it should be

.attr('value', selectValue );

or better

.val( selectValue );

Comments

5

The attr an prop functions of jquery selector are for the attributes which don't have function for that selector. if you want to check or change the value of an input you should call val() function of selector. change your code to this :

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);

Comments

3

change:

 jQuery('input[name="prop_sc_AttachmentType"]').attr('value="' + selectvalue + '"');

to

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);

Comments

3

Firstly your attr() syntax is incorrect, it should be attr('prop', 'value');, secondly to set a value, you should use val() anyway. Try this:

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);

Example fiddle

Comments

3

attr() change to val()

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);

http://jsfiddle.net/uz79M/3/

Comments

3

it is

.attr( attributeName, value )

so it will be

jQuery('input[name="prop_sc_AttachmentType"]').attr('value',selectvalue);

Comments

2

You should use val() to change it's value:

jQuery('input[name="prop_sc_AttachmentType"]').val(selectValue);

The attribute property is better used for other attributes like title, name etc.

Comments

2

Instead of

    if (selectvalue !== "select") {
        jQuery('input[name="prop_sc_AttachmentType"]').attr('value="' + selectvalue + '"');
    }

Use

  if (selectvalue !== "select") {
        jQuery('input[name="prop_sc_AttachmentType"]').val( selectvalue);
    }

Fair trade !:)

1 Comment

Wow ... 8 answers before i finished typing :D !
2

You have to use .val() function,

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue); //standard

Or you can correct your code :

jQuery('input[name="prop_sc_AttachmentType"]').attr('value', selectvalue);

FIDDLE DEMO

Comments

2

Here is working code

and here is fiddle

jQuery(document).ready(function () {
    var selectvalue = jQuery("select#AttachmentTypeSelect").val();
    jQuery("select#AttachmentTypeSelect").change(function () {
        alert("d")
        selectvalue = jQuery("select#AttachmentTypeSelect").val();
        alert(selectvalue);
        if (selectvalue !== "select") {
            $('input[name="prop_sc_AttachmentType"]').attr('value', selectvalue);
        }
    });
});

Comments

2

Use following :

jQuery(document).ready(function () {
    var selectvalue = jQuery("select#AttachmentTypeSelect").val();

    jQuery("select#AttachmentTypeSelect").change(function () {
        selectvalue = jQuery("select#AttachmentTypeSelect").val();

        if (selectvalue !== "select") {
        jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);
        }
    });
});

Comments

2

You can set the value directly with the .val methode.

Replace this:

jQuery('input[name="prop_sc_AttachmentType"]').attr('value="' + selectvalue + '"');

with

jQuery('input[name="prop_sc_AttachmentType"]').val(selectvalue);

4 Comments

It is good form to add an explanation to an answer when providing code itself. While your code may be correct, unless the OP understands why, it is really giving a man a fish, not teaching them to fish - which is what SO is all about.
Yes, i would edit my answer after posting, but i see that so many people also gives the true answer.. ;)
I came across this because it had been flagged as a poor quality answer. It doesn't matter how many other answers there are - your answer is a statement that depicts YOU. If you want to answer, answer it as if there were no other answers at all. At the end of the day, one answer will be accepted. Do the best you can to make it yours :)
Thank you, i will do it better ;)
1

try this:

jQuery(document).ready(function () {
        var selectvalue = jQuery("select#AttachmentTypeSelect").val();
        jQuery("select#AttachmentTypeSelect").change(function () {
            alert("d")
            selectvalue = jQuery("select#AttachmentTypeSelect").val();
            alert(selectvalue);
            if (selectvalue !== "select") {
                $('input[name="prop_sc_AttachmentType"]').attr('value', selectvalue);
            }
        });
    });

Comments

0

Perhaps you are not using attr function correctly:

this should be like this

$('selector').attr({value:desiredValue});

or

$('selector').attr('value',desiredValue);

But for setting values jQuery provides

$('selector').val(desiredValue);

Comments

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.