0

I have input tags on the page that are generated dynamically using JS. They are initialized using $("#input1").val(value). How can I find specific input elements base on text?

The values are initialized as follows:

$("input[name='DeviceIP']").each(function(index,elem){$(elem).val(value)});

The solution I am using now is to select all the inputs I want to inspect and then using find.

$("[name='DeviceIP']").filter(function(index, elem) {
    var res = false;
    var _this = this;
    $("[name='DeviceIP']").each(function(index2, elem2) {
        if(elem2 !== _this) {
            if(_this.value === elem2.value) {
                errMessage = "error text";
                res = true;
            }
        }
    });
    return res;
}

I looked at the question here but the ":contains" didn't find them for some reason(maybe because there is no value attribute?)

Community
  • 1
  • 1
Randall Flagg
  • 4,834
  • 9
  • 33
  • 45

2 Answers2

1

"$("input[name='DeviceIP'][value='your_value_here']")

element.value is also an attribute, so You can define it in Your query ;)

Still, you shouldn't perform such query very often if You have a lot of elements.

I would also suggest You to create map, with values as keys, and nodes as values.

grzesiekgs
  • 463
  • 2
  • 11
  • Like I said in my question I am initializing the values with val() which doesn't add the attribute to the DOM. therefor I can't use this approach. I added a more detailed example to make it more clear. here is an updated fiddle: https://jsfiddle.net/kef3073a/2/ – Randall Flagg Nov 30 '16 at 13:45
  • @RandallFlagg Yes You can ;) The point is that attribute defined in HTML is initial value (in this case). When You modify that property with JS, it doesn't change what's defined in HTML, but it actually changes the 'visible value' (what you can see with Your eyes). So basically : var input = document.querySelector('[name="DeviceIP"]') var $input = $(input) input.value = 'qwe' $input.val() // -> qwe – grzesiekgs Dec 01 '16 at 11:51
  • If You would try to modify value attribute with jquery like : $input.attr('value', 'new value') than it wouldn't work! Attibute would change in HTML - but You wouldn't see any changes in Your input value. – grzesiekgs Dec 01 '16 at 11:53
0

Suppose you have an input field like

<input type="text" value="5" name="DeviceIP">
<input type="text" value="8" name="DeviceIP">

you want the element with specific value. so you can do this,

alert($("input[name='DeviceIP'][value='5']").val());

Here is the fiddle.
If your value is dynamic, say for example your searching value is 8.so you can do this in a way,

var val = 8;
alert($("input[name='DeviceIP'][value='"+val+"']").val());
Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
  • this is the same answer as grzesiekgs – synthet1c Nov 30 '16 at 13:02
  • No, in my last portion i give it for dynamic value also. Also i gave a fiddle to see live example. – Ataur Rahman Munna Nov 30 '16 at 13:06
  • @AtaurRahmanMunna Like I said in my question I am initializing the values with val() which doesn't add the attribute to the DOM. therefor I can't use this approach. I added a more detailed example to make it more clear. here is an updated fiddle: https://jsfiddle.net/kef3073a/2/ – Randall Flagg Nov 30 '16 at 13:22