1

I am having a problem where I need to change the HTML inside of a <button></button>. Here are the relevant lines of HTML:

<input class="form-control" type="text" id="subject" />
<button type="button" class="btn" id="btv"><p>Text 1</p></button>
<button type="button" class="btn" id="btc"><p>Text 2</p></button>

And this is the jQuery:

$('.btn').click(function(){
    var text = $('.btn p').html();
    $('#subject').val(text);
});

All I have managed to do is make the first text show no matter which button I press. I really need a bit of help.

2
  • 1
    Sorry, but why you have an paragraph on your Button? Commented Aug 30, 2017 at 19:31
  • because you select all the elements, not the one that was clicked.... Commented Aug 30, 2017 at 19:41

4 Answers 4

3

First of all paragraph inside button in not valid HTML.

But in this case also you can make your code working through the use of $(this) along with .children() like below:-

$(document).ready(function(){
  $('.btn').click(function(){
    $('#subject').val($(this).children('p').text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="form-control" type="text" id="subject" />
<button type="button" class="btn" id="btv"><p>Text 1</p></button>
<button type="button" class="btn" id="btc"><p>Text 2</p></button>

Note:-

Make sure that jQuery library added before your script code.

If you are adding your script code on top of page then it should be wrapped inside $(document).ready(function(){..});, but if you are adding script code at the bottom of the page then it's not necessary (in both case jQuery library need to be added before code)

A valid HTML example is like below:-

$('.btn').click(function(){
  $('#subject').val($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<input class="form-control" type="text" id="subject" />
<button type="button" class="btn" id="btv">Text 1</button>
<button type="button" class="btn" id="btc">Text 2</button>
</body>

Sign up to request clarification or add additional context in comments.

Comments

1

$('.btn').click(function(){
var text = $(this).html();
$('#subject').val(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<input class="form-control" type="text" id="subject" />
<button type="button" class="btn" id="btv">Text 1</button>
<button type="button" class="btn" id="btc">Text 2</button>
</body>

Comments

0
$('#subject').val( $(this).text() )

1 Comment

Code-only answers are often unhelpful in pointing to why your solution is helpful. Please include an explanation for why your code solves the issue.
0

Use text and context

$('.btn').click(function(){
    $('#subject').val($(this).text());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.