2

I use instead of I simply wish to get the value/content of the selected/active button, with jQuery, on click button and onload page.

These are my buttons:

<button type="button" class="green test">Button 1</button>
<button type="button" class="green active test">Button 2</button>

I know I must to use $(".test:button") selector, but I don't know how to get the button content

6
  • Hi guys, the problem is I have more button with the same class and I need to know the value of SELECTED button. In my example I have the 'Button 2' with class = "green active test". Is there a way to know the value of a selected button OR a button which contains "active" attribute in class? Commented Aug 8, 2013 at 9:43
  • check my edit on how to get the button containing active Commented Aug 8, 2013 at 11:11
  • Hi @Anton, the problem is that I have more buttons with the same "active" class, e.g.: <button class="green active">Button 1</button> <button class="green">Button 2</button> <button class="red active">Button 3</button> <button class="red">Button 4</button> So, I cannot only use .active selector Commented Aug 9, 2013 at 8:40
  • So you want to select a button which has green or red and active? Commented Aug 9, 2013 at 8:42
  • yes! I wish to select more classes: e.g.: 'green' AND 'active' Commented Aug 9, 2013 at 11:05

4 Answers 4

4

Use .text()

$(document).ready(function(){

    alert($('.test[type="button"]').text());

    $('.test[type="button"]').on('click',function(){
         alert($(this).text());
    });

});

DEMO

Edit

var text = $('button.active').text();

Edit

var redsText = $('button.red.active').text();
var greensText = $('button.green.active').text();
Sign up to request clarification or add additional context in comments.

Comments

1
$('button').text();

or

$('button').html();

Comments

1

Try

 $('button.test').click(function() {
        alert($(this).text());
    });

WORKING DEMO

Comments

0
<head> 
<script type="text/JavaScript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<button type="button" class="green test">Button 1</button>
<button type="button" class="green active test">Button2</button>
<script>
$(function(){
$(".green").click(function(){
$(this).addClass('active');

    $(this).siblings().removeClass("active")
alert($(this).text());
})
 })
</script>

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.