How to find input element id by value?

How to get the identifier of an input element based on its value? Values ​​will always be unique, and there will only be seven. I tried this:

$('#wrapper').find("input[value='"+value+"']").each(function(){
    return this.id;
});

But nothing comes back!

+3
source share
5 answers

Try

$ (this) .id nope, this.id works, there is no need to create a jQuery object for the identifier.

or

$(this).attr('id')

NTN

EDIT: This might work:

$('#wrapper').find("input[value='"+value+"']").attr('id');
+6
source

You write return this.id;... Return to where? You just return the value from anonymous functions, and I don’t see where you ever tried to use it. So the answer is:

var idYouAreSearchingFor = $('#wrapper').find("input[value='"+value+"']").attr('id');
+4
source

:

$('#wrapper').find("input[value='"+value+"']").each(function(){
        return  $(this).attr("id") 
});

http://jsfiddle.net/5xsZt/

edit: this.id, . . : : http://jsfiddle.net/5xsZt/3/

+3

You can solve this problem using filter . Like this:

$('#wrapper input').filter(function() {
    return $(this).val() == value;
}).each(function() {
    return this.id;
});
+1
source

Here's a version that will definitely work in all major browsers:

function getInputWithValue(wrapper, value) {
    var inputs = wrapper.getElementsByTagName("input");
    var i = inputs.length;
    while (i--) {
        if (inputs[i].value === value) {
            return inputs[i];
        }
    }
    return null;
}

var wrapper = document.getElementById("wrapper");
var input = getInputWithValue(wrapper, "some value");
window.alert(input.id);
+1
source

Source: https://habr.com/ru/post/1744129/


All Articles