1

Trying to figure out if and how I can get the specific value by only using the class name. Is it even possible?

All selects are generated dynamically and have the same class name. all the submit buttons have the same class name as well.

<div>
<select class="mystat" name="mystat" style="width: 140px">
    <option value="1" selected="selected">option 1</option>
    <option value="2">option 2</option>
    <option value="3">option 3</option>
</select>
<button class="go-btn" type="submit">Update</button>
</div>


$(document).on('click', '.go-btn', function(){
    var mystat = $('.mystat').val();
    alert('Value is ' + mystat);
});

Fiddle

Any help is greatly appreciated

1
  • do you have a different submit per select? Commented Aug 24, 2017 at 0:27

2 Answers 2

1

Use DOM navigation relative to $(this).

$(document).on('click', '.go-btn', function() {
  var mystat = $(this).siblings('.mystat').val();
  alert('Value is ' + mystat);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select class="mystat" name="mystat" style="width: 140px">
  <option value="1" selected="selected">option 1</option>
  <option value="2">option 2</option>
  <option value="3">option 3</option>
 </select>
  <button class="go-btn" type="submit">Update</button>
</div>
<div>
  <select class="mystat" name="mystat" style="width: 140px">
  <option value="1" selected="selected">option 1</option>
  <option value="2">option 2</option>
  <option value="3">option 3</option>
 </select>
  <button class="go-btn" type="submit">Update</button>
</div>
<div>
  <select class="mystat" name="mystat" style="width: 140px">
  <option value="1" selected="selected">option 1</option>
  <option value="2">option 2</option>
  <option value="3">option 3</option>
 </select>
  <button class="go-btn" type="submit">Update</button>
</div>

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

Comments

0

Try this, because select tag is in the front of the button in each block

var mystat = $(this).prev().val();

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.