Jquery and selectors

Joined
Sep 4, 2022
Messages
160
Reaction score
16
Hello Helpers !

I have a html part to select by JQuery :

HTML:
<section id="home">
<div class="main-title"> home </div>

<br> text ... text ... text <br><br>

</section>

<section id="account">
<div class="main-title"> Account </div>

<br> text ... text ... text <br><br>

</section>

I am using JQuery , and my aim is to change all texts after the div main-title, for now , I can't manage to do it,
trying many 'selectors' solution.

Do you have any good ways to achieve ?

Jquery selectors are :

JavaScript:
$('tag .class #id').function...
 
Joined
Jul 4, 2023
Messages
591
Reaction score
78
Have you tried this way?
JavaScript:
$(document).ready(function() {
  $('#home').html($('.main-title').prop('outerHTML') + 'Lorem ipsum text1');
  $('#account').html($('.main-title').prop('outerHTML') + 'Lorem ipsum text2');
});

HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<section id="home">
  <div class="main-title"> home </div>
  text ... text ... text
</section>

<section id="account">
  <div class="main-title"> Account </div>
  text ... text ... text
</section>

<script>
  $(document).ready(function() {
    $('#home').html($('.main-title').prop('outerHTML') + 'Lorem ipsum text1');
    $('#account').html($('.main-title').prop('outerHTML') + 'Lorem ipsum text2');
  });
</script>

<style>
  section {
    margin-bottom: 1.7rem; /* instead of <br><br> */
  }
  section div.main-title {
    margin-bottom: .9rem; /* instead of <br>  */
  }
</style>

or
JavaScript:
  $(document).ready(function() {
    $('#home .text').text('Lorem ipsum text1');
    $('#account .text').text('Lorem ipsum text1');
  });

HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<section id="home">
  <div class="main-title"> home </div>
  <div class="text">
    text ... text ... text
  </div>
</section>

<section id="account">
  <div class="main-title"> Account </div>
  <div class="text">
    text ... text ... text
  </div>
</section>

<script>
  $(document).ready(function() {
    $('#home .text').text('Lorem ipsum text1');
    $('#account .text').text('Lorem ipsum text2');
  });
</script>

<style>
  section {
    margin-bottom: 1.7rem; /* instead of <br><br> */
  }
  section div.main-title {
    margin-bottom: .9rem; /* instead of <br>  */
  }
</style>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top