• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 103
  • Last Modified:

jQuery Selector Question

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
Howard Bash
Asked:
Howard Bash
2 Solutions
 
Terry WoodsIT GuruCommented:
This seems to work:
$("div[class$='MyTarget']")

Open in new window

0
 
MrunalCommented:
Here is the list of all types of selectors:

https://api.jquery.com/category/selectors/
0
 
Howard BashSenior Software EngineerAuthor Commented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Julian HansenCommented:
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
 
Howard BashSenior Software EngineerAuthor Commented:
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
 
Julian HansenCommented:
$('div[id$="RootAspMenu"]')

Open in new window

0
 
Howard BashSenior Software EngineerAuthor Commented:
Great stuff.  Thanks folks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now