0

How do you change the name of text within a div that your selector is inside of? In other words, I'd like to click on a button and change the text of the text above the button (within the same div).

For example, in my html, there is a button and a title above it. Once the button is clicked, I'd like the title above it to change to the id of the button.

This problem is similar to this question, but I'm having difficulty changing the text within the div that the selector is in.

Below is my attempt to grab the id of the button, and change the text within the div container.

Javascript (my attempt) (codepen here):

$('.btn.btn-default').on('click', function() {
  var desired_name = $(this).attr('id')
  $('#title').find($(this)).html(desired_name);
});

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p id="title">Temporary Button Title That Should be 'true_name1'</p>
<button class="btn btn-default"  id="true_name1">NAME</button>
</div>


<div>
<p id="title">Temporary Button Title That Should be 'true_name1'</p>
<button class="btn btn-default"  id="true_name2">NAME</button>
</div>
Community
  • 1
  • 1
Chris
  • 5,444
  • 16
  • 63
  • 119
  • you shouldn't give 2 elements the same id – Aya Salama Sep 14 '16 at 21:33
  • Please note that you are using two elements with the same ID ("title"). You should change these to classes (be sure to update your selectors). – johnniebenson Sep 14 '16 at 21:33
  • Consider using [data attributes](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes) for adding additional data to element rather than using the id attribute. ` – Preston S Sep 14 '16 at 21:46

4 Answers4

1

Have you tried $(this).siblings("p") This will select the <p> above the button

Pipe
  • 2,379
  • 2
  • 19
  • 33
1

Have you tried jquery .prev()

https://api.jquery.com/prev/

$('.btn.btn-default').on('click', function() {
    var desired_name = $(this).attr('id')
    $(this).prev().html(desired_name);
});
sidarcy
  • 2,958
  • 2
  • 36
  • 37
0

You can use the jQuery method siblings() to select the <p> element inside your <div> container.

However I recommend using a class as a unique selector inside your container.


$('.btn.btn-default').on('click', function() {
  $(this).siblings(".desired-name").text($(this).attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p class="desired-name">Temporary Button Title That Should be 'true_name1'</p>
  <button class="btn btn-default" id="true_name1">NAME</button>
</div>


<div>
  <p class="desired-name">Temporary Button Title That Should be 'true_name1'</p>
  <button class="btn btn-default" id="true_name2">NAME</button>
</div>
Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
0

Change the ID's to classes for starters. Then target them like this:

$('.btn.btn-default').on('click', function() {
  var desired_name = $(this).attr('id');
  $(this).prev(".title").text(desired_name);
});
johnniebenson
  • 540
  • 3
  • 9