0

I'm using below javascript to set value to cell in a tabular form.

<script type="text/javascript">
function eventListener(pThis)
{

$('input[name="f04"]').change(function(){

var that = this;
var vRow = "f10_"+pThis.id.substr(pThis.id.indexOf('_')+1);

$.post("wwv_flow.show", 
       {p_request      : "APPLICATION_PROCESS=GET_AMOUNT",
        p_flow_id      : $v("pFlowId"),
        p_flow_step_id : $v("pFlowStepId"),
        p_instance     : $v("pInstance"),
        x01            : $(this).val()
        },
        function(data){ 
              $('input[id="f10_0002"]').val(data);
        },
        "text"
      );
});

}
</script>

I need to use value in var vRow instead of f10_0002 in $('input[id="f10_0002"]').val(data);.

I have tried with $('input[id=vRow]').val(data); and $('input[id='"'+vRow+'"']').val(data);

But doesn't work. javascript is not worked in these situations. but $('input[id="f10_0002"]').val(data); is worked.

how could i use value in var vRow for this ?

4
  • 2
    Don't use $("input[id=" + value + "]")...just use $("#" + value) Commented Jan 23, 2013 at 7:01
  • @lan why ? can you explain the reason ? Commented Jan 23, 2013 at 7:05
  • 1
    # is the equivalent of matching an element by ID. That is its purpose in jQuery selection. If you look at the jQuery selectors page ( api.jquery.com/category/selectors ), you'll see there are several special characters to help with selecting elements. As I said, # is for ID, . is for class, etc. So instead of having jQuery parse the string "input[id=value]" and do a lot of extra processing, you can help jQuery do it more efficiently by simply using #. IDs are supposed to be unique and the method document.getElementById, which I'm sure jQuery uses internally, is "better" Commented Jan 23, 2013 at 7:10
  • 1
    @lan post your comment as a answer. i'll accept it. Commented Jan 23, 2013 at 7:13

2 Answers 2

1

While using the selector input[id=value] is technically right (because of the jQuery attribute selector), it isn't necessary. The main reason is because you're targeting an element by ID. Javascript has a special method that is especially fast, which I'm sure jQuery uses internally ( document.getElementById ) when you specify you're selecting by ID. To trigger the selection of an element by ID, you can use the # character at the beginning of the selector string. Since you are trying to use the value of a variable as well, you can concatenate "#" and vRow, like:

$("#" + vRow).val(data);

# is the equivalent of matching an element by ID. If you look at the jQuery selectors page ( http://api.jquery.com/category/selectors ), you'll see there are several special characters to help with selecting elements. As I said, # is for ID.... is for class, etc. So instead of having jQuery parse the string "input[id=value]" and do a lot of extra processing, you can help jQuery do it more efficiently by simply using #.

Sign up to request clarification or add additional context in comments.

2 Comments

@lan i use this javascript for change event of f04 row in my tabular form. this row is a autocomplete row. when i select item from autocomplete list, this javascript is not working. value is not set to the relevant field. but when i type item name with out selecting it from autocomplete list, it works. what could be the issue ?
This my code. Download it from here
1

Try:

$('input[id="' + vRow + '"]').val(data);

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.