user2956599
user2956599

Reputation: 1

dojo issue to check if div has a value

I am using dojo 1.8

I have got below html code snippet:

<div id="abc" class= "callToActionItems">
</div>

<div id="abc" class= "callToActionItems">
</div>

<div id="abc" class= "callToActionItems">
  <P>I have some value</P>
</div>

I have a requirement whereby I need to check if out of 3 div which one has a value and then do some stuff for those div having no content and set it to display none.

I tried below code:

require([
    "dojo/query",
    "dojo/domReady!"
], function(query){
    query(".callToActionItems").forEach(function(node, index, arr){
        console.log(node); 
        if (node.innerHTML) {

        } else{
            domStyle.set(node, "display", "none");
        }
    });
});

Here node.innerHTML returns the entire html but I only want the value.

I could not find a method on node object to get the actual value.

Any help is much appreciated.

Upvotes: 0

Views: 493

Answers (3)

qunshan
qunshan

Reputation: 57

you can try node.innerText instead of node.innerHTML here. BTW: firefox can't support innerText.

Upvotes: 0

MiBrock
MiBrock

Reputation: 1100

Try it like this:

require(["dojo/query","dojo/dom-style", "dojo/domReady!"], function(query,domStyle){
     query(".callToActionItems").forEach(function(node, index, arr){

   if(node.innerHTML){
     alert('I have a child');
    }
else{
   alert('I have none - I'm out');
   domStyle.set(node, "display", "none");
    }
  });
});

Here's the fiddle for the Example above : http://jsfiddle.net/fJh4x/

Regards, Miriam

Upvotes: 1

Kolban
Kolban

Reputation: 15276

Rather than use "innerHTML", to examine the text of the HTML contained within the DIV consider asking for the list of children and if that list is 0 length (i.e. if has no children), then the DIV is empty.

for example

query(".callToActionItems").forEach(function(node) {
   if (query("> *", node).length == 0) {
   } else {
      domStyle.set(node, "display", "none");
   }
}));

Upvotes: 0

Related Questions