0

HTML

<div id = "btnsdhd" class="btn-group">
    <button type="button" id="31" value="SD" class="btn btn-default">SD</button>
    <button type="button" id="32" value="HD" class="btn btn-default">HD</button>
</div>

JS

var btnsdhdDiv = $('#btnsdhd');

var getsdhd = function () {

  // Get all sdhd's from buttons with .active class
  var sdhd = $('button.active', btnsdhdDiv).map(function () {
    return $(this).val();
  }).get();

  // If no sdhd's found, get sdhd's from any button.
  if (!sdhd || sdhd.length <= 0) {
    sdhd = $('button', btnsdhdDiv).map(function () {
        return $(this).val();
    }).get();   
  }

  return sdhd;
};

The above code captures the values of the buttons and places it in the variable getsdhd

If none of the buttons are selected by default it captures both the values (SD,HD) I want to modify the if statement and add a third value NC to the variable when to no buttons are selected I want it to look like (SD,HD,NC) when no buttons are slected. I tried to use the .push() method but that didnt work. Any leads would be helpful.

2
  • Where did you use push? Commented Aug 10, 2015 at 13:26
  • @epascarello In the If statement, after the get method. Commented Aug 10, 2015 at 13:28

2 Answers 2

1

push() can not be chained in your case since push returns the new length, not the updated array. You need to add it below the get().

var btnsdhdDiv = $('#btnsdhd');

var getsdhd = function () {

  // Get all sdhd's from buttons with .active class
  var sdhd = $('button.active', btnsdhdDiv).map(function () {
    return $(this).val();
  }).get();

  // If no sdhd's found, get sdhd's from any button.
  if (!sdhd || sdhd.length <= 0) {
    sdhd = $('button', btnsdhdDiv).map(function () {
        return $(this).val();
    }).get()
    sdhd.push("NC");   
  }

  return sdhd;
};

console.log(getsdhd());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "btnsdhd" class="btn-group">
    <button type="button" id="31" value="SD" class="btn btn-default">SD</button>
    <button type="button" id="32" value="HD" class="btn btn-default">HD</button>
</div>

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

1 Comment

Perfect!! That exactly what I was looking for. I was trying the push method on getsdhd variable itself which now I realise is stupid. Thank you :)
0

You are not invoking the getsdhs function. If you invoke your function it will work. I used console.log to test if it works.

JS

var btnsdhdDiv = $('#btnsdhd');

var getsdhd = function () {

  // Get all sdhd's from buttons with .active class
  var sdhd = $('button.active', btnsdhdDiv).map(function () {
    return $(this).val();
  }).get();

  // If no sdhd's found, get sdhd's from any button.
  if (!sdhd || sdhd.length <= 0) {
    sdhd = $('button', btnsdhdDiv).map(function () {
        return $(this).val();
    }).get();
    sdhd.push("NC");
  }

  console.log(sdhd);
  return sdhd;
}();

Fiddle: https://jsfiddle.net/ABr/p23cb5dk/1/

2 Comments

Well @espascarello, I missed the "NC" part ;-) Added it to my answer.
And now getsdhd is not a method like the OP wanted. OP did not show all their code on how it was being used.

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.