Mikhail Mikhail - 5 months ago 49
jQuery Question

jQuery If slect option label = ... set input value

I have select and some inputs (range + text). I need to do when I choose one of select, input get values but the code works only with first choice. When I change my choice value doesn't changed. What should I correct?

$(document).ready(function () {
$("div.roword select").change( function() {
var text = $(this).find("option:selected").text();
if (text = "60x90") {
$("input#height, input#heightPlus").attr('value', '60');
$("input#width, input#widthPlus").attr('value', '90');
$("input#height, input#width").focus();
$("input#height, input#width").blur();
} else
if (text = "100x150") {
$("input#height, input#heightPlus").attr('value', '100');
$("input#width, input#widthPlus").attr('value', '150');
$("input#height, input#width").focus();
$("input#height, input#width").blur();
} else
if (text = "120x180") {
$("input#height, input#heightPlus").attr('value', '120');
$("input#width, input#widthPlus").attr('value', '180');
$("input#height, input#width").focus();
$("input#height, input#width").blur();
}
});
});

Answer Source

Convert:-

if (text = "60x90") {

To:-

if (text == "60x90") { //or if (text === "60x90") {

And so-on for others

Because = is assignment-operator not comparison-operator.

And

change

$("input#height, input#heightPlus").attr('value', '60');

To:-

$("input#height, input#heightPlus").val(60);

So-on for other attr('value') also......

Complete code need to be like this:-

$(document).ready(function () {     
    $("div.roword select").change( function() {
        var text = $(this).find("option:selected").text();
        if (text == "60x90") {
            $("input#height, input#heightPlus").val(60);
            $("input#width, input#widthPlus").val(90);
            $("input#height, input#width").focus();
            $("input#height, input#width").blur();
        }
        else if (text == "100x150") {
            $("input#height, input#heightPlus").val(100);
            $("input#width, input#widthPlus").val(150);
            $("input#height, input#width").focus();
            $("input#height, input#width").blur();
        }
        else if(text == "120x180") {
            $("input#height, input#heightPlus").val(120);
            $("input#width, input#widthPlus").val(180);
            $("input#height, input#width").focus();
            $("input#height, input#width").blur();
        }
    });
});