Solved

jQuery Selector Question

Posted on 2016-09-22
7
57 Views
Last Modified: 2016-09-24
I am trying to find a jQuery command to retrieve an DIV whose Class ends in MyTarget.  So far I haven't found an example which works.
0
Comment
Question by:Howard Bash
7 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 41811825
This seems to work:
$("div[class$='MyTarget']")

Open in new window

0
 
LVL 22

Expert Comment

by:Mrunal
ID: 41811984
Here is the list of all types of selectors:

https://api.jquery.com/category/selectors/
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 41812366
I tried the following on a SharePoint page:  $("div[class$='RootAspMenu']").length

The returned value was 0.  I use the length to check if the selector returned an object.

Also,  I searched the DOM view and found the div tag with a class = 'wxyZ_RootAspMenu'

If I look for that using $('#wxyZ_RootAspMenu').length I get back the value 1.

Please not that the prefix (wxyZ) changes which is why I need the kind of selecting.

IMPORTANT!!!
I realize that that value is the id value not the class and tried  $("div[id$='RootAspMenu']").length which also returned 0.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41812453
This works for me
<!doctype html>
<html>
<title>Selector Test</title>
<head>
</head>
<body>
<div class="'wxyZ_RootAspMenu"></div>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
	var x = $('div[class$="RootAspMenu"]');
	console.log(x.length);
});
</script>
</body>
</html>

Open in new window

Are you sure there are no errors in the console that might be preventing the code from running?
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 41812570
What if that value is not a class value, but the DIVs id value?

$('id[class$="RootAspMenu"]') didn't return the object.  I did see that the above works with classes.
0
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41812637
$('div[id$="RootAspMenu"]')

Open in new window

0
 
LVL 1

Author Closing Comment

by:Howard Bash
ID: 41812661
Great stuff.  Thanks folks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question