1

I have:

mytext = jQuery('#usp-title').val();

then I do

if(jQuery("#datafetch h2 a").text() == mytext) {

But one title could be:

Space Mission just as well as space mission

I cannot standardise the text format for other reasons therefore I am really looking at comparing two strings regardless of their capitalisation.

UPDATE

The thing is that I am using the input field as a search so the user could type space mission while the post title would be Space mission so the search result wouldn't be exact and i need the exact phrase instead.

Complete code:

<div id="datafetch"></div>

function fetch(){
  jQuery('#datafetch').empty();
  mytext = jQuery('#usp-title').val();
    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', exactwords:  mytext },
        success: function(data) {
            jQuery('#datafetch').html( data );
        if(jQuery("#datafetch h2 a").text().toLowerCase().trim() == mytext.toLowerCase().trim()) {
            jQuery("#componi").attr("disabled", "disabled").hide();
        } else {
                jQuery("#componi").removeAttr("disabled", "disabled").show();
        }
            if(jQuery("#datafetch h2 a").text() != mytext) {
            jQuery("#fatto").hide();
            jQuery('#datafetch').empty();
        }
        }
    });

}

It's trickier actually because the search is coming from the text input, so it's about the query of the ajax i believe more than capitalisation, just a thought. The latest code above gives no results by typing:

space mission while the post title is Space mission

UPDATE 2

Post article title:

Space Mission

User searches for

space mission

Console.log(data) gives correct result

   <ul class="list-unstyled margin-top-80">
       <li>
            <h2><a target="_blank" href="example.com"">Space Mission</a></h2>
       </li>
   </ul>

Yet this doesn't happens:

jQuery('#datafetch').html( data );

if I insert the right words Space Mission does tho

26
  • String#toLowerCase does not work for you? Commented Apr 9, 2017 at 8:36
  • @NinaScholz update the question Commented Apr 9, 2017 at 8:42
  • Possible duplicate of JavaScript: case-insensitive search Commented Apr 9, 2017 at 8:45
  • please add some use cases to make clear, what you want. Commented Apr 9, 2017 at 8:47
  • it looks more like a ajax question, than a string problem. does it work with the exact input and search? Commented Apr 9, 2017 at 8:53

6 Answers 6

8

Covert both toLowercase()

"Space Mission".toLowerCase() == "space mission".toLowerCase()

In context of your question

if(jQuery("#datafetch h2 a").text().toLowerCase() == mytext.toLowerCase()) {

Trimming in case user enters trailing or leading space, as suggested by @charlietfl

if(jQuery("#datafetch h2 a").text().trim().toLowerCase() == mytext.trim().toLowerCase()) {
Sign up to request clarification or add additional context in comments.

15 Comments

this is ok, was trying it, thanks. I have update the question, my fault as i wasn't too clear about.
might also want to trim them for safety
yeah but then you will search for a title like spacemission and there isn't any title like that
@rob.m trimming only removes whitespace on the ends, not between characters
also could be whitespace in html, not just user input
|
1

Get the input result using jquery

var string = jQuery("#datafetch h2 a").text()

and then you can try any of the following -

Simple string comparison : converting both to same case :

string.toUpperCase() === "String".toUpperCase()

If you are fine with regex(notice the i option in regex) :

var otherString = /String/i
otherString.test(string)

There is "match" as well :

string.match(/STRING/i)

match returns null value when no match found

Comments

1

The accepted answer is correct and it does answer my original question, and that's why I accepted it. However, the issue was solved by using a setTimeout(function() as there was a little delay between the data populating the div and the check for the 2 value comparisons.

function fetch(){
        jQuery.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'post',
            data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
            success: function(data) {
                    jQuery("#datafetch").html(data);
                    setTimeout(function() {
                        var text1 = jQuery("#datafetch").find("h2").find("a").html();
                        var text1B = text1.toLowerCase();
                        var text2 = jQuery('#usp-title').val();
                        var text2B = text2.toLowerCase();
                        if(text1B == text2B) {
                            jQuery("#componi").attr("disabled", "disabled").hide();
                        } else {
                            jQuery("#componi").removeAttr("disabled", "disabled").show();
                            jQuery("#fatto").hide();
                            jQuery('#datafetch').empty();
                        }
                    }, 1000);
            }
        });
}

UPDATE

Using .promise().done() was better

function fetch(){
    var text1;
    var text1B;
    var text2;
    var text2B;
        jQuery.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'post',
            data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
            success: function(data) {
                    jQuery("#datafetch").html(data).promise().done(function(){
                        text1 = jQuery("#datafetch").find("h2").find("a").html();
                        text1B = text1.toLowerCase();
                        text2 = jQuery('#usp-title').val();
                        text2B = text2.toLowerCase();
                        if (text1B != text2B) {
                            jQuery("#componi").removeAttr("disabled", "disabled").show();
                            jQuery("#fatto").hide();
                            jQuery('#datafetch').empty();
                        } else if (text1B == text2B) {
                            jQuery("#componi").attr("disabled", "disabled").hide();
                        }
                    });
            }
        });
}

UPDATE TWO

This is what I finally used

function fetch(){
  jQuery.ajax({
    url: '<?php echo admin_url('admin-ajax.php'); ?>',
    type: 'post',
    data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
    success: function(data) {
      var text2;
      var text2B;
      text2 = jQuery('#usp-title').val();
      text2B = text2.toLowerCase();
      jQuery("#datafetch").html(data).promise().done(function(){
        jQuery("#datafetch ul li h2 a").each(function() {
          var $this = jQuery(this);
          if ($this.text().toLowerCase() !== text2B) {
            $this.parent().parent().hide();
          } else if (text1B == text2B) {
            jQuery("#componi").attr("disabled", "disabled").hide();
          }
        });
      });
    }
  });
}

2 Comments

no need for setTimeout if you just wrap the response in $() and work inside that
@charlietfl i resolved it by using .promise().done and i have updated this answer
0

You will require to convert both strings to lowercase or uppercase and then do the comparison.

    var mytext = jQuery('#usp-title').val();
    if(jQuery("#datafetch h2 a").text().toLowerCase() == mytext.toLowerCase()) 
    {

    }

Comments

0

Convert both strings to upper or lower case

Comments

0

You can do like this

"Space Mission".replace(/\s+/g, "").toLowerCase() == "space    mission".replace(/\s+/g, "").toLowerCase()

The left hand side will be equal to spacemission same as the expression on the right sade

It will first remove any white space even in between words & will convert to lower case. Removing white space is necessary because space mission is not same as space mission

1 Comment

yeah but then you will search for a title like spacemission and there isn't any title like that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.