3

i am trying to select value for the selected button like the following :

html code :

<div class="span3">
            <div>DEBIT/CREDIT</div>
            <div class="btn-group" data-toggle="buttons-radio" id="investadjust-debitcredit-button">
                <button name="debitCredit" type="button" id="credit-button" class="btn active" value="CREDIT" >CREDIT</button>
                <button name="debitCredit" type="button" id="debi-button" class="btn" value="DEBIT" >DEBIT</button>
            </div>
        </div>

javascript code :

$("#investadjust-debitcredit-button>.active").val()

but i get only empty when i printed the above line after selecting one button.

what could be the reason.

9
  • Try encasing your code inside DOM Ready handler .. $(function() { // your code }); Commented Aug 10, 2013 at 7:10
  • Otherwise it works perfectly fine ..jsfiddle.net/sushanth009/g6YTd/2 Commented Aug 10, 2013 at 7:11
  • If the js code appers after that html there is no reason to use document.ready Commented Aug 10, 2013 at 7:15
  • i am using backbone, i am able to get other elements value but not able to get only the above field's value. Commented Aug 10, 2013 at 7:16
  • are you sure that the value of active button is not null just when you select it? Commented Aug 10, 2013 at 7:27

2 Answers 2

2

you need to place your code in

$(document).ready(function(){
// your jquery code
});
Sign up to request clarification or add additional context in comments.

Comments

1

As neo said you need to place your code in document ready function and use the class name of the button without spaces or you can add underscore between the words as well like this:

<button name="debitCredit" type="button" id="credit-button" class="btn_active" value="CREDIT" >CREDIT</button>

So, the code goes like:

$(document).ready(function(){
   alert("value "+$("#investadjust-debitcredit-button>.btn_active").val());
});

Good Luck!

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.