Jessica
Jessica

Reputation: 3769

How can I set an element HTML to the value of a variable with jQuery?

I am using the following code and it sets the value of the 2nd line correctly. When I use the debugger I can see that typeSelected HAS a text value I expect:

            var typeSelected      = $("#dialogType :selected").text();
            parent.find(".refType").html('aa');

When I replace it with:

            var typeSelected      = $("#dialogType :selected").text();
            parent.find(".refType").html(typeSelected);

Then the 2nd line doesn't get set to anything.

Is it wrong just for me to put .html(typeSelected) ?

Upvotes: 0

Views: 80

Answers (3)

patad
patad

Reputation: 9672

Not sure what type of element typeSelected is, but if it has a value, try:

var typeSelected = $("#dialogType").val();

.text() works great on elements with a textnode, such as <p>, <a> etc, while .val() is for values, such as <input> for example.

Upvotes: 0

Arief
Arief

Reputation: 6085

What kind of element is "#dialogType"?

Looking at ":selected" I think you should use .val() instead of .text() to get the correct value.

Upvotes: 0

Jirka Kopřiva
Jirka Kopřiva

Reputation: 3089

var typeSelected = $("#dialogType").val();
parent.find(".refType").html(typeSelected);

Upvotes: 1

Related Questions