Solved

jQuery Selector Question

Posted on 2016-09-22
7
60 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 55

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 55

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

Suggested Solutions

Title # Comments Views Activity
jQuery Dialog Autoresize Bug 2 25
Selected in an option list 13 21
message Alert on an empty search 10 23
Powershell script does not exit 4 30
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

820 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