469,336 Members | 5,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Get Element By Attached Property

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

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

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

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

I greatly appreciate any effort to help me.

Thanks,
Sisilla

Jan 26 '07 #1
3 2323


On Jan 27, 3:39 am, sisill...@gmail.com wrote:
I dynamically attached a property I arbitrarily called 'row' to an
<imgelement as follows -:

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

Is there any way to refer back to the <imgelement 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[i];
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.
--
Rob

Jan 27 '07 #2
On Jan 26, 8:05 pm, "RobG" <r...@iinet.net.auwrote:
On Jan 27, 3:39 am, sisill...@gmail.com wrote:
I dynamically attached a property I arbitrarily called 'row' to an
<imgelement as follows -:
var imgNode = document.createElement('img');
imgNode.row = iteration;
Is there any way to refer back to the <imgelement 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[i];
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.

--
Rob
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;
}
Feb 16 '07 #3
On Jan 26, 8:05 pm, "RobG" <r...@iinet.net.auwrote:
On Jan 27, 3:39 am, sisill...@gmail.com wrote:
I dynamically attached a property I arbitrarily called 'row' to an
<imgelement as follows -:
var imgNode = document.createElement('img');
imgNode.row = iteration;
Is there any way to refer back to the <imgelement 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[i];
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.

--
Rob
Thank you for your help, Rob.

Feb 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by nutso fasst | last post: by
9 posts views Thread by Harlan Messinger | last post: by
5 posts views Thread by moondaddy | last post: by
14 posts views Thread by webEater | last post: by
15 posts views Thread by caca | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.