Solution 1 :

You are selecting the element with the id and saying that should not have the class. It should be done on the child elements.

console.log($("#main-content > *:not(.ignore)").text());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='main-content'>
  <div>test 1</div>
  <div class='ignore'>ignore test</div>
</div>

Problem :

I want to select a div with an id or class and at the same time exclude a id or class inside the div and get the content as string.

Here is my markup:

<div id='main-content'>
  <div>test 1</div>
  <div class='ignore'>ignore test</div>
</div>

I tried $('#main-content:not(.ignore)').text();

It is selecting the div with the id main-content, but the class ignore is still in the string. What am I doing wrong?

Here is a fiddle example.

Comments

Comment posted by epascarello

You are saying that #main-content does not have the class

Comment posted by How to Ask

Please include all relevant code here on Stack Overflow, not only on another site. See

Comment posted by Ajay

@Heretic Monkey Sorry, next time I will do that.

Comment posted by Ajay

Thant you epascarello. So I was saying that I wanted to select the id “main-content” which didn’t have the class “ignore”. Damn, I thought it was selecting the id “main-content” without the content of the class “ignore”. Thank you again.

By