Get Element By Attached Property

S

sisilla14

I dynamically attached a property I arbitrarily called 'row' to an
<img> element as follows -:

var imgNode = document.createElement('img');
imgNode.row = iteration;

Is there any way to refer back to the <img> element using the property
name 'row' similar to the following -:

var rows = document.getElementsByPropertyName("row");

I greatly appreciate any effort to help me.

Thanks,
Sisilla
 
R

RobG

I dynamically attached a property I arbitrarily called 'row' to an
<img> element as follows -:

var imgNode = document.createElement('img');
imgNode.row = iteration;

Is there any way to refer back to the <img> element using the property
name 'row' similar to the following -:

var rows = document.getElementsByPropertyName("row");

There is no getElementsByPropertyName in the W3C DOM standards, however
you have a couple of other options. You can store references to your
img elements in an object or array, e.g.

var imgStore = {};
var imgNode = document.createElement('img');
imgNode.row = iteration;
imgStore[imgNode.row] = imgNode;

Of course imgNode.row must be unique.

Alternatively, you could sift through all the elements in the document
looking for the one with the attribute you're after, e.g.

function getElementsByPropertyName( tag, propName, value)
{
var el, els = document.getElementsByTagName(tag);
var i = els.length;
while (i--) {
el = els;
if (el.getAttribute(propName) == value) return el;
}
}

If you want to sift just img elements, then:

var x = getElementsByPropertyName('img', 'row', '2');

might do the trick. If you want any kind of element, use:

var x = getElementsByPropertyName('*', 'row', '2');


Don't forget feature detection and to allow for old browsers.
 
S

sisilla14

I dynamically attached a property I arbitrarily called 'row' to an
<img> element as follows -:
var imgNode = document.createElement('img');
imgNode.row = iteration;
Is there any way to refer back to the <img> element using the property
name 'row' similar to the following -:
var rows = document.getElementsByPropertyName("row");

There is no getElementsByPropertyName in the W3C DOM standards, however
you have a couple of other options. You can store references to your
img elements in an object or array, e.g.

var imgStore = {};
var imgNode = document.createElement('img');
imgNode.row = iteration;
imgStore[imgNode.row] = imgNode;

Of course imgNode.row must be unique.

Alternatively, you could sift through all the elements in the document
looking for the one with the attribute you're after, e.g.

function getElementsByPropertyName( tag, propName, value)
{
var el, els = document.getElementsByTagName(tag);
var i = els.length;
while (i--) {
el = els;
if (el.getAttribute(propName) == value) return el;
}

}

If you want to sift just img elements, then:

var x = getElementsByPropertyName('img', 'row', '2');

might do the trick. If you want any kind of element, use:

var x = getElementsByPropertyName('*', 'row', '2');

Don't forget feature detection and to allow for old browsers.


Thank you very much for your help, Rob.

var imgs = document.getElementsByTagName('img');
if (imgs)
{
var arrayImgs = [];
for (var x=0; x < imgs.length; x++)
{
if(imgs[x].isImgNode == 'true')
arrayImgs.push(imgs(x));
}
for (var y=0; y < minusImgs.length; y++)
arrayImgs[y].row = y + 1;
}
 
S

sisilla14

I dynamically attached a property I arbitrarily called 'row' to an
<img> element as follows -:
var imgNode = document.createElement('img');
imgNode.row = iteration;
Is there any way to refer back to the <img> element using the property
name 'row' similar to the following -:
var rows = document.getElementsByPropertyName("row");

There is no getElementsByPropertyName in the W3C DOM standards, however
you have a couple of other options. You can store references to your
img elements in an object or array, e.g.

var imgStore = {};
var imgNode = document.createElement('img');
imgNode.row = iteration;
imgStore[imgNode.row] = imgNode;

Of course imgNode.row must be unique.

Alternatively, you could sift through all the elements in the document
looking for the one with the attribute you're after, e.g.

function getElementsByPropertyName( tag, propName, value)
{
var el, els = document.getElementsByTagName(tag);
var i = els.length;
while (i--) {
el = els;
if (el.getAttribute(propName) == value) return el;
}

}

If you want to sift just img elements, then:

var x = getElementsByPropertyName('img', 'row', '2');

might do the trick. If you want any kind of element, use:

var x = getElementsByPropertyName('*', 'row', '2');

Don't forget feature detection and to allow for old browsers.


Thank you for your help, Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top