0

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?

3
  • you need to iterate through the inputs and filter Commented Apr 30, 2013 at 10:34
  • have you tried $("input[value^='001']") ..?? Commented Apr 30, 2013 at 10:36
  • Can't you give your input an id, or just select the element based on its name? Commented Apr 30, 2013 at 10:38

6 Answers 6

1

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

$("#asd").click(function (){
    $("input[name='input-1']").val("001-2007");
});
Sign up to request clarification or add additional context in comments.

1 Comment

It is probably a snippet, that shows the issue of a selector not selecting what it supposed to.
0

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();
 });
});

Comments

0

Its one of infamous JavaScript issues, try

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

Notice the Single and Double quotes is swapped arround

Comments

0

You want to do like

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

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

Comments

0

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

Comments

0

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.

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.