Pergunta

Hey there i am using this little jquery:

$("area.imagefield[data-name='" + searchText + "']").each(function() {

to check if the value of the attribute "data-name" matches exactly the variable "searchText".

But i need to check if the value of the attribute "data-name" is index of the variable "searchText", so using the

.indexOf

What i tried:

$("area.imagefield[data-name=]").each(function() {
    if ($(this).attr("data-name").indexOf(searchText) != -1) {
        console.log("match");
    } else {
        console.log("no match");
    }

But there is no log in the console... :/ any idea´s ?

Foi útil?

Solução

Actually your code looks good except one typo.
Remove the equal sign after data-name otherwise it's an invalid css selector:
So area.imagefield[data-name=] should be area.imagefield[data-name]

Outras dicas

Try this way:

$("#imagefieldID").each(function() {
    if ($(this).attr("data-name").indexOf(searchText) != -1) {
        console.log("match");
    } else {
        console.log("no match");
});

jQuery data()

Is the normal way of working withdata attributes in jQuery, and probably what you would want to use.

$('area.imagefield').each(function (i) {
    if ($(this).data('name')===searchText) {
        console.log('match '+i);
    } else { console.log('no match '+i); }
});

made a fiddle: http://jsfiddle.net/filever10/mau93/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top