0

Is there any way to get object formated like this from html via button click?

var Movie = [{id:"tt3783958", title:"La La Land", type:"Comedy, Drama, Music", year:"2016"}];

And then whit next button click, to append next object like this?

var Movie = [ {id:"tt3783958", title:"La La Land", type:"Comedy, Drama, Music", year:"2016"}, {id:"tt0488120", title:"Fracture", type:"Crime, Drama, Mystery", year:"2007"} ];

Live Demo: https://jsfiddle.net/q94eemn8/1/

6
  • What object do you want to make? Commented Jun 3, 2017 at 16:04
  • I want to make object like var Movie, just from button click "Add" Commented Jun 3, 2017 at 16:05
  • That object already exists in Output, so that does not make much sense. What do you want to have happen when you click the button? Commented Jun 3, 2017 at 16:06
  • 1
    Or are you trying to collect the details from the table row and append them into an existing object? Commented Jun 3, 2017 at 16:08
  • Exacly, I want to collect the details from table row and append them to existing object. Commented Jun 3, 2017 at 16:10

1 Answer 1

1

Here is a way you can do something like this. It's not entirely clear what you're looking to accomplish, so I took some guesses.

Working Example: https://jsfiddle.net/Twisty/tre7dx8a/

Added add as a class for each button in HTML.

JavaScript

$(function() {
  var Movies = [];
  $(".btn.btn-outline-success").click(function(e) {
    if ($(this).hasClass("add")) {
      var row = $(this).closest("tr");
      var movie = {};
      movie.id = row.find("td:eq(0)").html();
      movie.title = row.find("td:eq(1)").html();
      movie.year = row.find("td:eq(2)").html();
      movie.type = row.find("td:eq(3)").html();
      Movies.push(movie);
      console.log("Added Movie:", movie);
      $(this).html("Remove").toggleClass("add remove");
    } else {
      var id = $(this).closest("tr").find("td:eq(0)").html();
      var index = -1;
      $.each(Movies, function(k, v) {
        if (v.id == id) {
          index = k;
        }
      });
      if (index > -1) {
        Movies.splice(index, 1);
        console.log("Removed Movie:", id);
        $(this).html("Add").toggleClass("add remove");
      }
    }
  });
});

When Add is clicked, a movie object is created and populated with the items from the <td> in that <tr>. I used the :eq() selector to specifically identify each element. I then used .html() to read the content of the element. I could have also used .text().

I then considered that the user may make a mistake or change their mind, so I set the button to toggle itself to a Remove. This has a similar action, but will find and remove the movie from the storage array.

You can move them to local storage in a similar manner. Might be worthwhile writing a function to send the data back and forth to/from local storage.

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

1 Comment

Exacly what I was looking for!

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.