1

The structure is:

  1. You click on a cell,
  2. The pop-up window appears,
  3. You type something on the input fields.

What I want is to store the typed data into the cell which was clicked.

This could be perfectly done with Angular.js, but I need to do it using Javascript only (no frameworks, libraries etc.).

Here's my table with the pop-up window where the input fields are.

enter image description here

I have been trying to store the data simply by using .innerHTML method, but it just deletes the dates and inserts my text.

  var cell = document.getElementsByTagName('td');
  for (var i=0; i<cell.length; i++) {
     cell[i].onclick = function() {
        var data = this.getAttribute('data-cell');
        editEvent = document.getElementById('addevent');//The pop-up window
        editEvent.style.cssText ='display: block;';
        this.style.position = 'relative';

        this.innerHTML = " "//??? I want the inputs data to be inserted here

I also have those input data stored in localStorage. Perhaps, there's an easier way to transfer the data from localStorage into the cell? Because it is exactly what I need there.

Please, help! Thank you in advance!

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Alexandr Belov
  • 1,804
  • 9
  • 30
  • 43
  • 2
    Why in the world would you tag a question with "angular" if it is explicitly **not** intended to be done with angular? removing the tag – New Dev Jan 26 '15 at 21:35
  • I don't get it, what's the actual question here? – Paul S. Jan 26 '15 at 21:36
  • `.innerHTML` is not the attribute you're looking for. `.value` is what you need instead. – Giuseppe Crinò Jan 26 '15 at 21:38
  • Have you tried **setAttribute** **getAttribute**? Do you want to store it into cell dom element, or to show it in html?? – skobaljic Jan 26 '15 at 21:40
  • @Paul S. The actual question is how to store the data from input fields into the cell in the calendar. – Alexandr Belov Jan 26 '15 at 21:40
  • 1
    can you show the code for the Table (HTML)? – ViROscar Jan 26 '15 at 21:41
  • @ViROscar The table is created with JS, like that var table = document.createElement('table'); var tr = document.createElement('tr'); – Alexandr Belov Jan 26 '15 at 21:45
  • @skobaljic It seems like setAttribute getAttribute change nothing(( I think I need it to be stored into cell DOM element (in order to the user could edit it later just by clicking). – Alexandr Belov Jan 26 '15 at 21:49
  • i will recommend you to mention that because "innerhtml" will replace everything and i guess nobody here know how do you add the date to the cell. – ViROscar Jan 26 '15 at 21:51
  • Use html5 [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset), transfer all from localStorage. (but since you have everything in localStorage, why do you need it into dom at all?) – skobaljic Jan 26 '15 at 21:51
  • @skobaljic I need it to be displayed in the cell. It is supposed to be a sort of task in the calender. I stored the data in localStorage like, let's say: Event: Birthday Participants: Kurt, Dave, Kris... etc.... So, it is supposed to be shown in the calendar and be available for a user to edit it whenever he wants. – Alexandr Belov Jan 26 '15 at 22:04
  • But you do not want to show us html code for one cell. It should be something like `12.`... Tell us how it should look like without and with data inside. – skobaljic Jan 26 '15 at 22:08
  • @skobaljic It's the same as I wrote above. The cells also generated by JS: document.createElement('td'); Whole table generated via JS. The names of the Days were created by document.createTextNode(), etc. There's no actual "manual" mark-up, if we can say so. – Alexandr Belov Jan 26 '15 at 22:16
  • 1
    you can use the chrome console in order to see the code generated by js. if you can show that information, it will be helpful. – ViROscar Jan 26 '15 at 22:20
  • @skobaljic Ah, sorry, I didn't understand at once. Here it is
    "27, " "Tuesday"
    – Alexandr Belov Jan 27 '15 at 06:24

2 Answers2

2

You haven't (yet) posted the generated HTML, so I'm guessing you have only one HTML element per day. That's why when you use element.innerHTML, you change their whole content.

Create empty elements (div or span would do) next to the date elements.

Like so:

var date, dateBefore;
for(...) {
    // your existing date element
    dateBefore = date;
    date = document.createElement("div");
    // some code to insert the day into it, like date.innerHTML = "26";

    // a placeholder for your input
    datePlaceholder = document.createElement("div");

    // add the placeholder element to the DOM
    document.body.insertBefore(datePlaceholder, dateBefore);

    // insert your empty placeholder right before it, see also http://stackoverflow.com/questions/4793604/how-to-do-insert-after-in-javascript-without-using-a-library
    document.body.insertBefore(date, datePlaceholder);
}

You may want to to put both the date and datePlaceholder in the same div.

Edit: using this example, you would have to change your selector (td -> div) and make sure the placeholders are big enough so they "catch" the clicks. For instance, add a .placeholder class and make sure to give them a size in CSS:

.placeholder {
    height: 100%;
    width: 100%;
    /* debug border to make sure it takes the whole cell */
    border: 1px red solid;
}
pyb
  • 4,813
  • 2
  • 27
  • 45
2

You can use HTMLElement.dataset to read/write custom data attributes. An example app is HERE. You can also work with localStorage directly, than the unique ID would be date.

In order to show text/data into calendar cell, you need a placeholder. In my example each table cell is in this format:

<td>
    <div data-date="6-1-2015" class="box">
        <span class="day">6</span>
        <span class="text"></span> <!-- this is a placeholder -->
    </div>
</td>

This way it is easy to find clickable cells:

// Select clickable cells
var cells = document.querySelectorAll('.box');

than to bind click events (to open modal window):

// Bind clickable cells
for (var i = 0; i < cells.length; i++) {
    cells[i].addEventListener('click', function(e) {
        var thisCell = this;
        var thisDate = thisCell.dataset.date;
        var thisEvent = thisCell.dataset.event;
        var thisParticipants = thisCell.dataset.participants;
        var thisDescription = thisCell.dataset.description;
        date.innerHTML = thisDate;
        event.value = thisEvent || '';
        participants.value = thisParticipants || '';
        description.value = thisDescription || '';
        modal.style.display = 'block';
    });
};

Saving data is piece of cake:

// Bind save button
submit.addEventListener('click', function(e) {
    var thisDate = date.innerHTML;
    var thisEvent = event.value;
    var thisParticipants = participants.value;
    var thisDescription = description.value;
    var thisCell = document.querySelector('[data-date="' + thisDate + '"]');
    // Assign data to table cell
    thisCell.dataset.event = thisEvent;
    thisCell.dataset.participants = thisParticipants;
    thisCell.dataset.description = thisDescription;
    // Show on screen
    var textField = thisCell.querySelector('.text'); //-- This is our container
    textField.innerHTML = '\
        <span class="eve">' + thisEvent + '</span>\
        <span class="par">' + thisParticipants + '</span>\
        <span class="des">' + thisDescription + '</span>\
    ';
    modal.style.display = 'none';
});

and after filling data, table cell looks like this:

<td>
    <div data-date="6-1-2015" class="box" data-event="Event Title" data-participants="John, Robert" data-description="Football">
        <span class="day">6</span>
        <span class="text">
            <span class="eve">Event Title</span>
            <span class="par">John, Robert</span>
            <span class="des">Football</span>
        </span>
    </div>
</td>

For more info, please take a look at Fiddle, code is huge to paste it here.

skobaljic
  • 9,379
  • 1
  • 25
  • 51