2

I have an attribute which might look like this abcjQuery12345, these numbers are just an example.
Is there a way to determine an attribute which contains a substring in jQuery?

E.g. abcjQuery12344353452343452345="12"

The only thing I know is that the attribute will contain abc.

OArnarsson
  • 801
  • 1
  • 9
  • 25
Stir Zoltán
  • 4,033
  • 3
  • 16
  • 14

9 Answers9

2

You can loop through all the attributes and check if they contain the string you are looking for. With:

​<div customAttr="12" title="me"></div>
<div anotherAttr="Bear"></div>
<div yetAnotherAttr="Elephant"></div>

You could use:

var m = /another/i; //will look for 'another' in the attribute name (i-flag for case-insensitive search)

$('div').each(function(){
    var $this = $(this);
    $.each(this.attributes,function(){
        if (this.name.match(m)){
            $this.addClass('selected'); //either select the matching element
            $this.text(this.name + ' : ' + this.value); //or use its custom-attribute's value
        }
    });
});​

See a fiddle

m90
  • 11,434
  • 13
  • 62
  • 112
1

You can do it like this, Demo on JsFiddle

<div id="div1" myattr="hello" myAttr12342="hello1">Any text</div>​


el = document.getElementById('div1');
attrs=el.attributes
for (var i=0; i<attrs.length; i++)
{
    attrName = attrs.item(i).nodeName;
    attrValue = attrs.item(i).nodeValue; 
    //alert(attrs.item(i).nodeName);
    if(attrName.indexOf('myattr') != -1)
    {
        alert("Attr Name " + attrName + " Attr Value " + attrValue );
    }        
}
​

Adil
  • 146,340
  • 25
  • 209
  • 204
  • And what would this look like if my custom attribute is myattr123 and I only know that the attribute can contain myattr? – Stir Zoltán May 10 '12 at 12:35
  • I have updated my answer now it can match it custom attributes by matching some string pattern – Adil May 10 '12 at 17:19
1
var abcAttr, abcName;

​$.each($("#elementID")​[0].attributes, function(index, item) {
    if (item.nodeName.match(/^abc/)) {
        abcName = item.nodeName;
        abcAttr = item.nodeValue;
    }
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You can do this:

var customNum = 12345; // Switch this out as necessary.
var value = $('#element').attr('abcjQuery' + customNum);

Here's a working example.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • The problem is that I don't know the number – Stir Zoltán May 10 '12 at 12:33
  • If you don't know the number, then it sounds like the number is either irrelevant and shouldn't exist in the attribute name, or is part of the attribute value or some other attribute's value, and should be stored as such. – FishBasketGordo May 10 '12 at 12:38
  • Well the number is irrelevant, but the value of the attribute is. – Stir Zoltán May 10 '12 at 12:41
  • Or perhaps, if you didn't create the attribute, the number was appended to make it more difficult to access and modify the value. If that's the case, then I would be careful doing just that. There might be a better way to access the value of the attribute through whatever created it's API. – FishBasketGordo May 10 '12 at 13:23
0

Jquery.attr() method should do the trick

var value = $('#element').attr('abcjQuery' + someCustom);

but i suggest you to use data- attributes. like this by using Jquey.data()

<div id="div1" data-myattr="abcjQuery12345">Any text</div>​

for retriving :

var value = $('#div1').data('myattr');
alert(value):
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

You can use that :

$('a[@name^="abcjQuery"]')
Sameh Serag
  • 747
  • 1
  • 8
  • 22
0

if i understand your question here is your answer;

working example: http://jsfiddle.net/gRsxM/

/*!
 * listAttributes jQuery Plugin v1.1.0
 *
 * Copyright 2010, Michael Riddle
 * Licensed under the MIT
 * http://jquery.org/license
 *
 * Date: Sun Mar 28 05:49:39 2010 -0900
 */
if(jQuery) {
    jQuery(document).ready(function() {
        jQuery.fn.listAttributes = function(prefix) {
            var list = [];
            $(this).each(function() {
                var attributes = [];
                for(var key in this.attributes) {
                    if(!isNaN(key)) {
                        if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
                            attributes.push(this.attributes[key].name);
                        }
                    }
                }
                list.push(attributes);
            });
            return (list.length > 1 ? list : list[0]);
        }
    });
}


$(document).ready(function(){

    $('input').each(function(){
        var attrs = $(this).listAttributes();
         for(var i=0;i<attrs.length;i++){
            if(attrs[i].indexOf("abc")>-1) itemProccess($(this));
        }

    });
});


function itemProccess(item){
    console.log(item);
}
​
siniradam
  • 2,727
  • 26
  • 37
0

A simple function to get attribute value given a string contained in the attribute name:

function getVal(element,name){
   var pattern=new RegExp(name,'i');
   return $.grep($(element)[0].attributes, function(n){
       if (n.name.match(pattern)) return n.nodeValue;
   })[0].nodeValue;
}

DEMO

ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
-1
var myAttr = $("#idOfElement").attr("your_custom_attribute_name");
sampathsris
  • 21,564
  • 12
  • 71
  • 98