0

I want to do this loop using jquery but I can't do it.

This is my code

this.isFound = function (li, text){
  for(var i = 0; i<li.length; i++){
    if(li[i].innerHTML == text){
      this.showError("Sorry, You can't enter a string twice");
      return true;
      break;
    }
  }
  return false;
};

This is my JQuery loop

$.each(this.list, function(i, item){
  if(this.text == item.innerHTML){
    return true;
  }
});

How can I do that using each or grep or any function else in JQuery?! thanks in advance

Community
  • 1
  • 1
Mohamed Kamel
  • 93
  • 1
  • 13
  • and why you want it into each.. for loop is not bad. Also, $.each is for iterating over object whereas you are trying to iterate over list – Abhinav Mar 03 '17 at 17:35
  • `$("[someSelector] li").each(...)`. – Spencer Wieczorek Mar 03 '17 at 17:36
  • Possible duplicate of [Looping through list items with jquery](http://stackoverflow.com/questions/4511652/looping-through-list-items-with-jquery) – Abhinav Mar 03 '17 at 17:43
  • I the script that you wrote it. my question is how can I check an innerHTML of an element in the list inside each when I write something like that `if(li.innerHTML == text)` the innerHTML append the previous values not one value like here in the for loop – Mohamed Kamel Mar 03 '17 at 17:43
  • _"the innerHTML append the previous values not one value like here in the for loop"_ Can you include that description at Question? Do you mean all of the previous `.innerHTML` values that have been iterated? – guest271314 Mar 03 '17 at 17:56
  • yes I want one value to check if it equals like the for loop and I tried and searched many sites and references and can't do it. – Mohamed Kamel Mar 03 '17 at 17:59
  • @MohamedKamel Requirement at Question is not presently clear. What do you mean by _"the innerHTML append the previous values not one value like here in the for loop"_? Is expected result a single match, or multiple matches? Is expected result an array or a jQuery object? – guest271314 Mar 03 '17 at 18:00

1 Answers1

0

You can use $.grep() to return an array of matches, or .filter() to return a jQuery collection of elements, where the return value of the callback should be Boolean true or false

$.grep()

$(function() {
  var text = "abc";
  var res = $.grep($("li"), function(el, index) {
    return el.textContent === text
  });
  // `res` is an array containing one or more `DOM` elements
  console.log(res);
  res[0].style.color = "red";
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<ul>
  <li>abc</li>
  <li>def</li>
  <li>ghi</li>
</ul>

.filter()

$(function() {
  var text = "def";
  var res = $("li").filter(function(index, el) {
    return el.textContent === text
  });
  // `res` is a jQuery object containing one or more `DOM` elements
  // where jQuery methods can be chained
  console.log(res);
  res.css("color", "green");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<ul>
  <li>abc</li>
  <li>def</li>
  <li>ghi</li>
</ul>

If you are trying to determine if a given element has .textContent, and if true do stuff, you can use .is(), which returns a Boolean; either true or false

$(function() {
  var text = "ghi";
  var i = 0;
  var res = $("li").is(function(index, el) {
    i = index;
    return el.textContent === text
  });
  // `res` is a `Boolean`
  console.log(res);
  if (res) {
    alert(text + " has already been set");
    $("li").eq(i).css("color", "gold");
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<ul>
  <li>abc</li>
  <li>def</li>
  <li>ghi</li>
</ul>
guest271314
  • 1
  • 15
  • 104
  • 177