0

Hey guys I'm working on my new MVC app but... The function I want to have with this code doesn't work. When the user clicks on a Task, it has to appear in the paragraph. As you can see the function only works with the first table item.

 $(document).ready(function(){
        $("#btnAdd").click(function () {
            var task = $('#taskDescription').val();
            $("#paragraph").append(task, "    <button>-</button></br>");
        });
    });
.table-left {
            width: 30%;
            display: inline-block;
        }
        tbody {
            overflow: scroll;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table-left">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>Dienst</th>

        </tr>
        </thead>
        <tbody>
        
            <tr>
                <td>hey</td>
                <td><button id="btnAdd">+</button></td>
                <input type="hidden" id="taskDescription"   value="hey" />
            </tr>
            <tr>
                <td>hi</td>
                <td><button id="btnAdd">+</button></td>
                <input type="hidden" id="taskDescription"   value="hi" />
            </tr>
            <tr>
                <td>hello</td>
                <td><button id="btnAdd">+</button></td>
                <input type="hidden" id="taskDescription"   value="hello" />
            </tr>
            <tr>
                <td>bye</td>
                <td><button id="btnAdd">+</button></td>
                <input type="hidden" id="taskDescription"   value="bye" />
            </tr>
       
        </tbody>
    </table>
</div>
<p id="paragraph"></p>

Who knows what to do? I read this post: Why the jQuery Selectable plugin doesn't work with a foreach generated list?

But it doesn't seem to solve my problem

3 Answers 3

1

You should never have multiple elements with the same ID. An ID should ALWAYS be unique

Second, to get the value of the taskDescription that is associated with the clicked btnAdd, use $(this).closest('tr').find('.taskDescription').val()

$(document).ready(function() {
  $(".btnAdd").click(function() {
    var task = $(this).closest('tr').find('.taskDescription').val();
    $("#paragraph").append(task, "<button>-</button></br>");
  });
});

Working example of your code

$(document).ready(function() {
  $(".btnAdd").click(function() {
    var task = $(this).closest('tr').find('.taskDescription').val();
    $("#paragraph").append(task, "<button>-</button></br>");
  });
});
.table-left {
  width: 30%;
  display: inline-block;
}

tbody {
  overflow: scroll;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table-left">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Dienst</th>

      </tr>
    </thead>
    <tbody>

      <tr>
        <td>hey</td>
        <td><button class="btnAdd">+</button></td>
        <input type="hidden" class="taskDescription" value="hey" />
      </tr>
      <tr>
        <td>hi</td>
        <td><button class="btnAdd">+</button></td>
        <input type="hidden" class="taskDescription" value="hi" />
      </tr>
      <tr>
        <td>hello</td>
        <td><button class="btnAdd">+</button></td>
        <input type="hidden" class="taskDescription" value="hello" />
      </tr>
      <tr>
        <td>bye</td>
        <td><button class="btnAdd">+</button></td>
        <input type="hidden" class="taskDescription" value="bye" />
      </tr>

    </tbody>
  </table>
</div>
<p id="paragraph"></p>

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

3 Comments

Oh ofcourse an id should be unique.. Thanks for your fast reply! I appreciate it
Yes it does! :)
@daphne If any of the answers helped you, you should consider marking one of them as answer, so others users can see what solved your problem
0

Change ID To Class Change Jquery

$(document).ready(function(){
        $(".btnAdd").click(function () {

            var task = $(this).parent().next('input').val();
            $("#paragraph").append(task, "    <button>-</button></br>");
        });
    });

$(document).ready(function(){
        $(".btnAdd").click(function () {
        
            var task = $(this).parent().next('input').val();
            $("#paragraph").append(task, "    <button>-</button></br>");
        });
    });
.table-left {
            width: 30%;
            display: inline-block;
        }
        tbody {
            overflow: scroll;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table-left">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>Dienst</th>

        </tr>
        </thead>
        <tbody>
        
            <tr>
                <td>hey</td>
                <td><button class="btnAdd">+</button></td>
                <input type="hidden" class="taskDescription"   value="hey" />
            </tr>
            <tr>
                <td>hi</td>
                <td><button class="btnAdd">+</button></td>
                <input type="hidden" class="taskDescription"   value="hi" />
            </tr>
            <tr>
                <td>hello</td>
                <td><button class="btnAdd">+</button></td>
                <input type="hidden" class="taskDescription"   value="hello" />
            </tr>
            <tr>
                <td>bye</td>
                <td><button class="btnAdd">+</button></td>
                <input type="hidden" class="taskDescription"   value="bye" />
            </tr>
       
        </tbody>
    </table>
</div>
<p id="paragraph"></p>

Comments

0

in your button tag, change your id to class. and change # in your javascript to .
example :

<button type="button" id="blablabla"></button>, change to <button type="button" class="blablabla"> <br>

and in your javascript change :

$(#blablabla) to $(.blablabla)

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.