Pregunta

I've research this topic extensibly and I'm asking as a last resort before assuming that there is no wildcard for what I want to do.

I need to pull up all the text input elements from the document and add it to an array. However, I only want to add the input elements that have an id.

I know you can use the \S* wildcard when using an id selector such as $(#\S*), however I can't use this because I need to filter the results by text type only as well, so I searching by attribute.

I currently have this:

values_inputs = $("input[type='text'][id^='a']");

This works how I want it to but it brings back only the text input elements that start with an 'a'. I want to get all the text input elements with an 'id' of anything. I can't use:

values_inputs = $("input[type='text'][id^='']"); //or
values_inputs = $("input[type='text'][id^='*']"); //or
values_inputs = $("input[type='text'][id^='\\S*']"); //or
values_inputs = $("input[type='text'][id^=\\S*]"); 
//I either get no values returned  or a syntax error for these

I guess I'm just looking for the equivalent of * in SQL for JQuery attribute selectors. Is there no such thing, or am I just approaching this problem the wrong way?

¿Fue útil?

Solución

Actually, it's quite simple:

var values_inputs = $("input[type=text][id]");

Otros consejos

Your logic is a bit ambiguous. I believe you don't want elements with any id, but rather elements where id does not equal an empty string. Use this.

values_inputs = $("input[type='text']")
    .filter(function() { 
        return this.id != '';
     });

Try changing your selector to:

$("input[type='text'][id]")

I figured out another way to use wild cards very simply. This helped me a lot so I thought I'd share it.

You can use attribute wildcards in the selectors in the following way to emulate the use of '*'. Let's say you have dynamically generated form in which elements are created with the same naming convention except for dynamically changing digits representing the index:

id='part_x_name' //where x represents a digit 

If you want to retrieve only the text input ones that have certain parts of the id name and element type you can do the following:

var inputs = $("input[type='text'][id^='part_'][id$='_name']");

and voila, it will retrieve all the text input elements that have "part_" in the beginning of the id string and "_name" at the end of the string. If you have something like

id='part_x_name_y' // again x and y representing digits

you could do:

var inputs = $("input[type='text'][id^='part_'][id*='_name_']"); //the *= operator means that it will retrieve this part of the string from anywhere where it appears in the string. 

Depending on what the names of other id's are it may start to get a little trickier if other element id's have similar naming conventions in your document. You may have to get a little more creative in specifying your wildcards. In most common cases this will be enough to get what you need.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top