javascript - Jquery search for a value inside a div - Stack Overflow

I have thefollowing div, <div class="clinic_visit_list_entry animated fadeInUp"><d

I have the following div,

<div class="clinic_visit_list_entry animated fadeInUp">
    <div>Hepatitis B</div>
    <div>Screened: Yes</div>
    <div>Treated: Yes</div>
    <div>Referred: Yes British American Tobacco Kenya Clinic</div>
</div>
<div class="clinic_visit_list_entry animated fadeInUp">
    <div>Hepatitis B</div>
    <div>Screened: Yes</div>
    <div>Treated: Yes</div>
    <div>Referred: Yes British American Tobacco Kenya Clinic</div>
</div>

When I select a value from the following html select tag ,

<label>Select Service</label>
<select class="select_with_label" name="Service" id="select_service">
    <option selected="selected">Please select</option>  <option value="TB">TB</option>
    <option value="Hepatitis B">Hepatitis B</option>
    <option value="Hepatitis C">Hepatitis C</option>
    <option value="Overdose management">Overdose Management</option>
    <option value="Abscess">Abscess</option>
    <option value="Alcohol & drug abuse">Alcohol & drug abuse</option>
    <option value=" Cervical cancer" >Cervical cancer</option>
</select>

It should trigger the following jquery :

$("#select_service").change(function () {
     var value = $( "#select_service" ).val();
     //Search through the Div for the selected value 
});

Which should search through the clinic visit entry list if the selected value exists in the div. How can I implement the search through the div? (It should only search for the values which are already in the select option values)

I have the following div,

<div class="clinic_visit_list_entry animated fadeInUp">
    <div>Hepatitis B</div>
    <div>Screened: Yes</div>
    <div>Treated: Yes</div>
    <div>Referred: Yes British American Tobacco Kenya Clinic</div>
</div>
<div class="clinic_visit_list_entry animated fadeInUp">
    <div>Hepatitis B</div>
    <div>Screened: Yes</div>
    <div>Treated: Yes</div>
    <div>Referred: Yes British American Tobacco Kenya Clinic</div>
</div>

When I select a value from the following html select tag ,

<label>Select Service</label>
<select class="select_with_label" name="Service" id="select_service">
    <option selected="selected">Please select</option>  <option value="TB">TB</option>
    <option value="Hepatitis B">Hepatitis B</option>
    <option value="Hepatitis C">Hepatitis C</option>
    <option value="Overdose management">Overdose Management</option>
    <option value="Abscess">Abscess</option>
    <option value="Alcohol & drug abuse">Alcohol & drug abuse</option>
    <option value=" Cervical cancer" >Cervical cancer</option>
</select>

It should trigger the following jquery :

$("#select_service").change(function () {
     var value = $( "#select_service" ).val();
     //Search through the Div for the selected value 
});

Which should search through the clinic visit entry list if the selected value exists in the div. How can I implement the search through the div? (It should only search for the values which are already in the select option values)

Share Improve this question edited Sep 28, 2015 at 16:48 Artur Filipiak 9,1574 gold badges32 silver badges56 bronze badges asked Sep 28, 2015 at 16:31 H DindiH Dindi 1,5528 gold badges39 silver badges71 bronze badges 2
  • Search thru and then do what? – lshettyl Commented Sep 28, 2015 at 16:36
  • @lshettyl then it alerts if the value already exists in the Div , example if it finds Ascess in the div , it should alert you that it already exists in the div. – H Dindi Commented Sep 28, 2015 at 16:39
Add a ment  | 

3 Answers 3

Reset to default 3

You can use :contains selector

Description: Select all elements that contain the specified text.

$("#select_service").change(function () {
    var value = $(this).val();
    var div = $('.clinic_visit_list_entry div:contains("'+value+'")');
});

JSFiddle demo

Demo with an alert (OP ment to the question)

You could loop through all the divs and check if their content matches the selected value.

$("#select_service").change(function() {
    var value = $(this).val();
    $(".clinic_visit_list_entry div").each(function() {

        if ($(this).text().match(value)) {
            //Text found in div 
        }
    });
});

I would remend searching through the data that produced the HTML markup for the div, but if you must search the DOM you could use jQuery's find method. A plete, working example is below.

$("#select_service").change(function () {
  var value = $( "#select_service" ).val();
  
  //Search through the Div for the selected value
  var isFound = !!$('div.clinic_visit_list_entry').find('*:contains(' + value + ')').length;
  var output = document.createElement('div');
  
  output.innerHTML = 'Value ' + value + ' found: ' + isFound;
  document.body.appendChild(output);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="clinic_visit_list_entry animated fadeInUp"><div>Hepatitis B</div><div>Screened: Yes</div><div>Treated: Yes</div><div>Referred: Yes British American Tobacco Kenya Clinic</div></div><div class="clinic_visit_list_entry animated fadeInUp"><div>Hepatitis B</div><div>Screened: Yes</div><div>Treated: Yes</div><div>Referred: Yes British American Tobacco Kenya Clinic</div></div>

<br />

<label>Select Service</label>
<select class="select_with_label" name="Service" id="select_service">
  <option selected="selected">Please select</option>  <option value="TB">TB</option>
  <option value="Hepatitis B">Hepatitis B</option>
  <option value="Hepatitis C">Hepatitis C</option>
  <option value="Overdose management">Overdose Management</option>
  <option value="Abscess">Abscess</option>
  <option value="Alcohol & drug abuse">Alcohol & drug abuse</option>
  <option value=" Cervical cancer" >Cervical cancer</option>
</select>

<hr />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646978a4638028.html

相关推荐

  • javascript - Jquery search for a value inside a div - Stack Overflow

    I have thefollowing div, <div class="clinic_visit_list_entry animated fadeInUp"><d

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信