Select content from “div class” which contains certain words

109
January 27, 2021, at 2:40 PM

I have the following ajax-response which is generated by a server sided script:

<div class="item-title3">Testname</div>
<div class="item-level">120</div>
<div class="item-binding">40</div>
<div class="item-type">Feet</div>

Now i want to select the content of the div class which contains the "item-title" words somewhere in the div class name. So the number in the div class name shouldn´t be relevant (in this case the 3 of item-title3 shouldn´t be relevant).

In this example my code should grab "Testname", but it can´t find it. I think the siblings method is the problem here.

<script language="javascript">
jQuery(function($) {
  
  $("[tooltip-link]").each(function() {
    let $tooltip = $(this);
    let id = $tooltip.attr("data-id");
    $.ajax({ 
      url: "/datenbank/itemscript.php",
      type: "GET",
      data: {
        "var": id
      }
    }).then(function(data) {
      let $content = $(data);
      let title = $content.siblings('.item-title').text()
      $tooltip.tooltip({
        tooltipClass: "test",
        content: data
      });
       $("<div class=\"item-title\">" + title + "</div>").appendTo($tooltip);
    });
  });
    
});
</script>
<a tooltip-link data-id="12555" title=""></a>
Answer 1

In this case you can use jQuery wildcard class selector which contains item-title string.

<div class='hidden' style='display:none'></div>
let $content = $.parseHTML(data);
$('.hidden').empty();
$('.hidden').append($content);
$('.hidden').find('div[class*=item-title]').text();
Add one extra hidden div which holds the html response and use $.parseHTML method to parse the response string to html and append it to the hidden div. Make sure every time clear the hidden div content.

Wildcard selector selects elements which matches the string specified in the class attribute.

READ ALSO
I&#39;m not able to pass the state value to the method or function in React [closed]

I'm not able to pass the state value to the method or function in React [closed]

Want to improve this question? Add details and clarify the problem by editing this post

136
How to access array index of Cloud Firestore using query in Flutter?

How to access array index of Cloud Firestore using query in Flutter?

I have field users in document and this field contains two element in arrayI have to check specific two values are in this array

161
Getting an unwanted space between Nav Items in Navbar Angular

Getting an unwanted space between Nav Items in Navbar Angular

I am trying to create a sidenav bar with nav items in it which has sub menusI am able to create it but i am getting space between 2 navitems which have submenus

52
PHP Script INSERT INTO SELECT does not insert, with no errors [closed]

PHP Script INSERT INTO SELECT does not insert, with no errors [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

92