dpaluy
dpaluy

Reputation: 3715

How to find a div element by attribute?

I have the following HTML:

<body>
  <div id="contentPane">
    ...
    ...
    <div guidedhelpid="friendsuggestions">
    ...
  </div>
</body>

I need to do some changes in guidedhelpid. For example, $(#theElem).remove()

Note: I can use jQuery upon request.

What is the best way to do it?

Upvotes: 1

Views: 765

Answers (2)

MattDiamant
MattDiamant

Reputation: 8781

If you're using jQuery (upon request), then you can do $('div[guidedhelpid=friendsuggestions]')

For pure javascript, you can use this function to return an array of elements that contain that attribute name:

function getElementsByAttributeName(attr) {
    var arr_elms = document.body.getElementsByTagName("*"),
        elms_len = arr_elms.length,
        return_arr = [];

    for (var i = 0; i < elms_len; i++) {
       if(arr_elms[i].getAttribute(attr) != null){  
           return_arr.push(arr_elems[i]);
       }
    }

    return return_arr;
}

Upvotes: 4

Hemant_Negi
Hemant_Negi

Reputation: 2078

if using Jquery it is simply possible by

   var div = $('div[guidedhelpid="friendsuggestions"]');

Upvotes: 1

Related Questions