0

Checking if a title already exists. Is there a shorter way? Something like:

if(test.exists.inside('.title'){...

var x = 0;
var test = $('#test').text();
$('.title').each(function(){
if($(this).text() == test){x = 1;}
});
if(x == 1){console.log('exists');}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='title'>lorem</div>
<div class='title'>ipsum</div>
<div class='title'>lorema</div>
<div class='title'>ipsuma</div>

<div id='test'>lorem</div>
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • Have you considered ```document.title```? ```console.log(document.title)``` Origin [here](https://stackoverflow.com/questions/1057059/how-to-get-the-title-of-html-page-with-javascript) – tomerpacific Jan 13 '19 at 07:36
  • 1
    @tomerpacific, no, example pls – qadenza Jan 13 '19 at 07:37
  • What exactly is the purpose of "shorter"? Code golfing, or what do you think about using a more *appropriate* method if it takes more characters? (note that the jQuery library itself requires *tons* of characters :) ) – CertainPerformance Jan 13 '19 at 07:38
  • @CertainPerformance, `shorter` means `shorter`. less code for such a trivial thing – qadenza Jan 13 '19 at 07:40

2 Answers2

1

a lot of ways to do this :contains , filter() with indexOf() and filter() with text equal test.. depending on what you're trying to do

var x = 0;
var test = $('#test').text();
var Exists = $('.title:contains("'+test+'")').length;
console.log('Yes '+Exists+ ' title with text '+test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='title'>lorem</div>
<div class='title'>ipsum</div>
<div class='title'>lorema</div>
<div class='title'>ipsuma</div>

<div id='test'>lorem</div>

you can check also with for contains text check

$('.title:contains("'+test+'")').length > 0

OR for exact text check

$('.title').filter(function(){
  return $(this).text().trim() == test;
}).length > 0

Note: :contains and .indexOf search for the text contains text not the exact text .. by using $(this).text().trim() == test; this will return the exact text element

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
1

If you're just looking for shorter code, one option would be to invoke Array.prototype.some, and rather than store the result of .text() in a variable, invoke it every time, to save on characters typed:

if ([].some.call($('.title'), d => $(d).text() === $('#test').text())) {
  console.log('exists');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='title'>lorem</div>
<div class='title'>ipsum</div>
<div class='title'>lorema</div>
<div class='title'>ipsuma</div>

<div id='test'>lorem</div>

Personally, I'd prefer readability over code length:

const textToFind = $('#test').text();
const exists = Array.prototype.some.call(
  $('.title'),
  title => $(title).text() === textToFind
);
if (exists) {
  console.log('exists');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='title'>lorem</div>
<div class='title'>ipsum</div>
<div class='title'>lorema</div>
<div class='title'>ipsuma</div>

<div id='test'>lorem</div>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320