Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Detect If Div Class name starts with "modal" and has specific ID or just look for specific ID?

How would I check in JavaScript  (jQuery ok but not preferred) if a div class starts with the word "modal" and has a specific ID? So for the below example, I'd want to check if div started with "modal" and has an ID of "existingAppModalBox". I will have a list of IDs I need to check against but this will always be against a div with a class starting with the word "modal"? Or would it be easier to just check for the specific ID? My challenge is that these are modals so they have to be visible, thoughts?
<div class="modal in confirmationModal" tabindex="-1" role="dialog" id="existingAppModalBox" data-hard-modal="True" style="display: none;" aria-hidden="true">

Open in new window


Thanks!
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Please check out the script below:
<div class="modal in confirmationModal" tabindex="-1" role="dialog" id="existingAppModalBox" data-hard- modal="True" style="display: block;background-color:red" aria-hidden="true" onclick="CheckDiv(this)">
  123
</div>


<script>
  listOfIds = new Array();
  listOfIds[0] = "existingAppModalBox";

  function CheckDiv(divRef) {
    for (var i = 0; i <= listOfIds.length; i++) {
      if (divRef.id === listOfIds[i]) {
        alert("Id Match");
      } else {
        alert("No Match");
      }
    }
  }

</script>

Open in new window

You can see it in action at: http://jsfiddle.net/bspxcea2/

PS: I changed your Div tag a bit to test the scenario, I hope you got the idea how it can be done. I am not checking for modal in this snippet, let me know if it is a must have. If you just need to check against couple of classes, add them to the ListOfIDs array and you are good to go.
ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

Thank you both! Much appreciated!