1

So I have a list of items from a database which I have iterated through, and I made each item a modal where the user can edit each item, such as item->name, item->colour, item-title etc.

Each <input></input> has a class edit-title for the title, and edit-name for the name, and so on.

Since I'm using ajax to send this information to my database I have to use jQuery, so I know how to do this if it was one button that occurred once, but not multiple times.

Here is what I'm using to get the data:

var title = $('.e-title').val();
var id = $('.e-id').val();
var subject = $('.e-class').val();
var date = $('.e-due').val();
var description = $('.e-description').val();

But every time, only the first nth-child() gets picked, no matter which button I press. How would I maybe find the closest instances of those classes for jQuery to pick up?

HTML

<div class="e-modal-form">

        <label id="e-id" style="display:none;" class="e-id"> {{ $assignment->id }} </label>
        <h5>Title</h5> <input class="form-control e-title" type="text" value="{{ $assignment->title }}"/> <br>
        <h5>Subject<h5> <select class="form-control e-class" value="{{ $assignment->class }}"> <option> e.g. Chemistry @foreach([1,2,3,4] as $num) <option> {{$num}} </option> @endforeach </select> <br>
        <h5>Due Date</h5> <input class="form-control e-due" type="date" min="2016-01-02" value="{{ $assignment->created_at->format('Y-m-d') }}"> <br>
        <h5>Description</h5> <textarea class="form-control e-description" rows="4">{{ $assignment->description }}</textarea>
        <button class="e-btn-edit e-btn e-update-button">
           <span class="glyphicon glyphicon-floppy-save"></span> Save
        </button>

      </div>

My updateAssignment Function

function updateAssignment()
{

  var $container = $(this).closest('.e-modal-form');
  var title = $container.find('.e-title').val();
  var id = $container.find('.e-id').val();
  var subject = $container.find('.e-class').val();
  var date = $container.find('.e-due').val();
  var description = $container.find('.e-description').val();

  alert(title);

  $.get('/updateAssignment', {title: title, id: id, class: subject, date: date, description: description}, function()
  {


  });



}

How I'm trying to call it from the document.ready handler

$('.e-update-button').click(function() {

      updateAssignment();

 });
6
  • You need to use DOM traversal from the button which is clicked, but we need to see your HTML to show you how to reach those elements. Can you please edit your question to include it. Mar 2, 2016 at 13:57
  • I have updated my question @RoryMcCrossan Mar 2, 2016 at 13:59
  • You could try to add table id's to the id of the input? <input id="subject_33" />/
    – dmaij
    Mar 2, 2016 at 14:02
  • Worth mentioning that while the two (current) answers to the question use ".closest(..)", you could also use ".parent(..)" in this instance depending on your goals. See the jQuery docs for more info.
    – Sean
    Mar 2, 2016 at 14:06
  • @SeanClark true, although closest() is preferred as it is more rigid; should the structure of your HTML change then the JS will still work (assuming the selectors remained the same) Mar 2, 2016 at 14:31

2 Answers 2

3

To do this you can traverse the DOM from the button which was clicked to get the relevant inputs within the same div container. Try this:

$('.e-btn-edit').click(function() {
    var $container = $(this).closest('.e-modal-form');
    var title = $container.find('.e-title').val();
    var id = $container.find('.e-id').val();
    var subject = $container.find('.e-class').val();
    var date = $container.find('.e-due').val();
    var description = $container.find('.e-description').val();

    // work with the values here...
});

If the modal div is appended to the DOM after the page is loaded, then you would need to use a delegated event handler on the button, like this:

$(document).on('click', '.e-btn-edit', function() {
    // the rest of the above code...
});

Update

Given the logic flow in your update question, you need to amend your code like this:

function updateAssignment() {
    var $container = $(this).closest('.e-modal-form');
    var title = $container.find('.e-title').val();
    var id = $container.find('.e-id').val();
    var subject = $container.find('.e-class').val();
    var date = $container.find('.e-due').val();
    var description = $container.find('.e-description').val();

    // work with the values here...
});

$('.e-btn-edit').click(updateAssignment);
8
  • If I do $('.e-update-button').click(function() { updateAssignment(); }); where I have your first chunk of code exactly, it returns undefined, but if I place your code in the $(document).ready(); it works fine, how do I make it work in a function? Mar 2, 2016 at 14:16
  • I'm not sure what you mean; event handlers in jQuery have to go inside the document.ready handler Mar 2, 2016 at 14:17
  • I have a function updateAssignment(); where I put var $container = $(this).closest('.e-modal-form'); var title = $container.find('.e-title').val(); var id = $container.find('.e-id').val(); var subject = $container.find('.e-class').val(); var date = $container.find('.e-due').val(); var description = $container.find('.e-description').val();, when i alert(title), it returns as undefined Mar 2, 2016 at 14:19
  • That's because you need to pass the reference of the button to the updateAssignment() function as the scope of the this keyword has changed. Mar 2, 2016 at 14:22
  • How would I do that? Just a normal parameter? Mar 2, 2016 at 14:23
0

You need to traverse back to the parent which holds all the elements, And from there find all the required element and extract the value. The code would be like below

$('.e-btn').on('click',function(){
     var $parentDiv = $(this).closest('div.e-modal-form');

     var title = $parentDiv.find('.e-title').val();
     var id = $parentDiv.find('.e-id').val();
     var subject = $parentDiv.find('.e-class').val();
     var date = $parentDiv.find('.e-due').val();
     var description = $parentDiv.find('.e-description').val();

     //your stuff here
    });

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.