user2335460
user2335460

Reputation: 11

Jquery and input value selector issue

Html Code:

<input type="text" name="input-1" class="input-1" value="001 / 2007">
    <input type="button" id="asd" value="check">

JS Code

$(document).ready(function (){
    $("#asd").click(function (){
        alert($("input[value='001 / 2007']").val());
        $("input[value='001 / 2007']").val("001-2007");
    });
});

The value changed through Jquery is not available to input[value...] selector after the change, it is still selects based on the old value. Here is the live code http://jsfiddle.net/WwRVy/, So question is how to select input based on latest value?

Upvotes: 0

Views: 819

Answers (6)

Artyom Neustroev
Artyom Neustroev

Reputation: 8715

My guess is, that jQuery .val() method changes the property value, while jQuery selector works with attributes. There are several workarounds, i.e. using [0] notation of jQuery object and change its value attribute or change it with .attr():

$("input[value='001 / 2007']").attr("value", "001-2007");

FIDDLE

P.S.: You'd better change the way you select that element, i.e. assign an id or a class to it.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$(document).ready(function (){
    $("#asd").click(function (){
        var inputs = $('input').filter(function(){
            return $(this).val() == '001 / 2007';
        });
        alert(inputs.val());
        inputs.val("001-2007");
    });
});

Demo: Fiddle

Upvotes: 0

vinothini
vinothini

Reputation: 2604

You want to do like

  $("input[value=001 / 2007]").val("001-2007");

Modified jsfiddle link is http://jsfiddle.net/WwRVy/5/

Upvotes: 0

Sydwell
Sydwell

Reputation: 5024

Its one of infamous JavaScript issues, try

alert($('input[value="001 / 2007"]').val());

Notice the Single and Double quotes is swapped arround

Upvotes: 0

xdazz
xdazz

Reputation: 160833

Why not select base on the name or just the class?

$("#asd").click(function (){
    $("input[name='input-1']").val("001-2007");
});

Upvotes: 1

John
John

Reputation: 539

Try this

Html Code:

<input type="text" name="input-1" class="input-1" value="001 / 2007">
<input type="button" id="asd" value="check">

JS Code

$(document).ready(function (){
 $("#asd").click(function (){
    alert($("input").val());
    $("input").val();
 });
});

Upvotes: 0

Related Questions