Capture value of H1 and set on another H1 with jquery, with click

Asked

Viewed 1,318 times

0

I have the following situation, I have two H1 in different locations on the website, I would like to click the value of one H1, pass to the other.

  • Is there any relationship between them in the DOM tree?

2 answers

3


Imagining that the two H1 are on the same page, follows below with vc should do using Jquery:

function Capture(){
  var _text = $('#primeiro_h1').html();
  $('#segundo_h1').html(_text);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1 id="primeiro_h1">Seu primeiro H1</h1>
<h1 id="segundo_h1">Seu segundo H1</h1>
<a href="javascript:;" onclick="Capture();">Click para capiturar</a>

Now if the idea is to make the value change as it is clicked on some element it would be like this:

$('#bto_click').click(function(){
  var _text = $('#primeiro_h1').html();
  $('#segundo_h1').html(_text);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1 id="primeiro_h1">Seu primeiro H1</h1>
    <h1 id="segundo_h1">Seu segundo H1</h1>
    <a href="javascript:;" id="bto_click">Click para capiturar</a>

Any doubt is just shout!!!!

1

Follow the script for changing values between two H1:

$(document).ready(function() {
  $('h1').click(function() {

    var prevH1 = $(this).prev('h1').html();
    var nextH1 = $(this).next('h1').html();
    var thisH1 = $(this).html();

    if (nextH1) {
      $(this).html(nextH1);
      $(this).next('h1').html(thisH1);
    } else {
      $(this).html(prevH1);
      $(this).prev('h1').html(thisH1);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1>Lorem Ipsum</h1>

<h1>Título</h1>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.