3

How do I get the attribute's value from "data-column" in the outerHTML text?

$.each($groupingColumnUl[0].children, function(i1, o1) {
   alert(o1.outerHTML.prop('data-column');  //.attr('data-column');  //.find('data-column');
});

The actual value inside the outerHTML is

<li class='ui-state-default' data-column='Model'><span style='display:inline-block;' class='ui-icon ui-icon-close'></span>Model</li>

I want to get the value "Model"

4
  • 2
    why not just use $(o1).data("column")? BTW outerHTML returns a string not a jQuery object. data doc Commented Oct 2, 2013 at 16:25
  • $(o1.parent).data('column') Commented Oct 2, 2013 at 16:25
  • Here's an interesting article saying dont use "data()" function... lookfirst.com/2011/12/dont-use-jquery-data-method-use-attr.html Commented Oct 2, 2013 at 16:37
  • @fletchsod, that is a nearly 2 year old article and the situation that it points out has been fixed Commented Oct 3, 2013 at 9:25

4 Answers 4

2

Try.

$.each($groupingColumnUl[0].children, function(i1, o1) {
   alert($(o1).data('column'));
});

or (if older JQuery version) :

$.each($groupingColumnUl[0].children, function(i1, o1) {
   alert($(o1).attr('data-column'));
});
Sign up to request clarification or add additional context in comments.

3 Comments

o1 is already a DOM object so you cannot use get on it. Even if o1 was a jQuery object all you are doing is getting the DOM object and then rewrapping it with jQuery.
Sorry... mistake due to a lack of information... The OP is using DOM object with JQuery functions... =-= Edited but i still don't know if it's good.
Interesting. The attr() works even though I'm using JQuery version 2.0 but data() returned undefined. Looking at the jquery documentation, it said data() would return undefined if the data is not set but once set then data() will return the value. Interesting...
2

OuterHTML Text :

<input id ="UserControl" value ="New Custmer" defaultvalue="Cust415875" clientcontrolname = "CusterInfo">

Geting clientcontrolname (attribute) value from outerHTML :

    var strGrpAssoc = oGrpAssoc.outerHTML + "</input>";
    var xmlGrpAssoc = loadXMLString(strGrpAssoc.toLowerCase());
    var sClientControlName = xmlGrpAssoc.getElementsByTagName("input")[0].getAttribute("clientcontrolname"); 

function loadXMLString(txt) {
    if (window.DOMParser) {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(txt, "text/xml");
    }
    else // code for IE
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(txt);
    }
    return xmlDoc;
} 

Comments

0

outerHTML gives you a sting, not a jQuery object, so if you want to access the data attribute of an element via jQuery API, you can try:

alert($(o1.outerHTML).data('column')

1 Comment

if you will post your code to jsfiddle, we can try to discover this error.
0

Try the below

alert($(o1).data('column'));

1 Comment

o1 is not a jQuery object, it is a DOM object. Also this would get the parents data, OP was using outerHTML which just gets that dom object's html not the parents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.