anerjan
anerjan

Reputation: 25

javascript how to get html style's custom attribute value

I have html element with some inline styles plus on custom css property. now i can access every style attribute but not my custom style property.

here is my code.

<img id="myimg" class="ImgClass" style="overfolow:hidden;position:absolute;mycustomProp:100;top:15;left:20">

here i cannot get mycustomProp.

Any help?

Upvotes: 1

Views: 1460

Answers (3)

ѺȐeallү
ѺȐeallү

Reputation: 3017

check

$("#myDiv").hasAttribute(name)

get

$("#myDiv").getAttribute(name)

set

$("#myDiv").setAttribute(name)

delete

$("#myDiv").removeAttribute(name)

Upvotes: 0

FrancescoMM
FrancescoMM

Reputation: 2960

http://jsfiddle.net/phrjwgxk/

alert(getCustomStyle("myimg","mycustomProp"));


function getCustomStyle(theId,theStyle) {
    var styles=document.getElementById(theId).getAttribute("style").split(';');
    var astyle;
    for(var i=0;i<styles.length;i++) {
        astyle=styles[i].split(':');
        if(astyle[0]==theStyle) return (astyle[1]);
    }
    return undefined;
}

Upvotes: 1

Mandera
Mandera

Reputation: 2992

document.getElementById("myimg").getAttribute("style") to retrieve the attribute.

document.getElementById("myimg").setAttribute("style",<string>) can be used to change it.

Upvotes: 1

Related Questions