0

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?

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
dpaluy
  • 3,537
  • 1
  • 28
  • 42
  • I think [this](http://stackoverflow.com/questions/9496427/how-to-get-elements-by-attribute-selector-w-native-javascript-w-o-queryselector) could help you to make your own JS function ppure javascript no jquery. – woofmeow Aug 04 '13 at 19:15
  • 1
    possible duplicate of [Find all elements with a certain attribute value in jquery](http://stackoverflow.com/questions/4958081/find-all-elements-with-a-certain-attribute-value-in-jquery) – Felix Kling Aug 04 '13 at 19:15
  • 3
    I recommend to use `data-*` attributes so that your HTML is valid. – Felix Kling Aug 04 '13 at 19:16
  • `guidehelpid` is not a valid html attribute. What do you want to remove, the attribute, the value of it or the whole `div` element? Completely agree with @FelixKling . Didn't you find that out already? I found [this validator report through google](https://sitevalidator.com/html/error/attribute-guidedhelpid-not-allowed-on-element-div-at-this-point) – Francisco Presencia Aug 04 '13 at 19:19

2 Answers2

3

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;
}
MattDiamant
  • 8,561
  • 4
  • 37
  • 46
1

if using Jquery it is simply possible by

   var div = $('div[guidedhelpid="friendsuggestions"]');
Hemant_Negi
  • 1,910
  • 1
  • 20
  • 25