Randall Flagg
Randall Flagg

Reputation: 5098

Find elements by text

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?)

Upvotes: 0

Views: 93

Answers (2)

Ataur Rahman Munna
Ataur Rahman Munna

Reputation: 3917

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

Upvotes: 0

grzesiekgs
grzesiekgs

Reputation: 463

"$("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.

Upvotes: 1

Related Questions