Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to jQuery select a label inside a div? [duplicate]

My dom looks like this:

<div class="edit-section slideDown">
  <form>
  <div class="info-header">
    <div class="acct-section-title clearfix">
      <h3 class="acct-section-title-txt pull-left">Meals</h3>
      <button type="button" class="pull-right btn-close icon-close"></button>
    </div>
  </div>
  <div id="flash-msg"></div>


  <ul class="info-list info-list-form">

    <div class="col-sm-4 info-label"><label for="delivery_name">Name</label></div>
    <div class="col-sm-8 info-value">
      <div class="form-group mb-0">
        <input type="text" name="delivery_name" id="delivery_name" class="form-input">
      </div>
      ...

How do I use jQuery to check if the input at the bottom has the dom element:

<label for="delivery_name">Name</label>

What's hte best way to check that?

$('.edit-section') gets me the outer element but then how do I get the label tag?

like image 943
Jwan622 Avatar asked Dec 08 '22 20:12

Jwan622


2 Answers

you can use find for it.

check the following snippet

console.log($(".edit-section").find("label[for =delivery_name]").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="edit-section slideDown">

  <div class="info-header">
    <div class="acct-section-title clearfix">
      <h3 class="acct-section-title-txt pull-left">Meals</h3>
      <button type="button" class="pull-right btn-close icon-close"></button>
    </div>
  </div>
  <div id="flash-msg"></div>


  <ul class="info-list info-list-form">

    <div class="col-sm-4 info-label">
      <label for="delivery_name">Name</label>
    </div>
    <div class="col-sm-8 info-value">
      <div class="form-group mb-0">
        <input type="text" name="delivery_name" id="delivery_name" class="form-input">
      </div>

    </div>
like image 82
Geeky Avatar answered Jan 06 '23 06:01

Geeky


How do I get the label tag?

The most straightforward approach is:

$('label[for="delivery_name"]');

How do I use jQuery to check if the input at the bottom has the label?

If $('label[for="delivery_name"]').length < 1, then it isn't there.

like image 38
Rounin - Glory to UKRAINE Avatar answered Jan 06 '23 06:01

Rounin - Glory to UKRAINE