How to sent a value from javascript to a form and get the value as form id

SreRoR :

I have a fullcalender script. I have already created a event for today(for eg), now i want to edit it, so on click on todays date i have to send a value from a javascript to my form to edit this event.

This is my onclick javascript function

var initialize_calendar;
initialize_calendar = function() {
  $('.calendar').each(function(){
      eventClick: function(event, jsEvent, view) {
        $.getScript(event.edit_url, function() {
          $('#event_id').val(event.id);
        });
      }
    });
  })
};
$(document).on('turbolinks:load', initialize_calendar);

The value from $('#event_id').val(event.id); i'm getting in my form as 32. you can see it in form input field below form

<form class="simple_form edit_event" id="edit_event_#{#event_id}" action="/securities/events/#{#event_id}" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
        <input type="hidden" value="32" name="event[id]" id="event_id">
</form>

My problem is

The same event id 32, i want to append in my form id as id="edit_event_32" and in form action action="/securities/events/32" please help.

Lal :

You can use the jQuery parent() method to get the parent of an element. After getting the parent, you can just set the id attribute using the jQuery attr() method. The code would be as below.

$('#event_id').parent().attr('id','edit_event_'+event.id);

Now, to set the form action, you can still use the same technique. The code would be as below.

$('#event_id').parent().attr('action','/securities/events/'+event.id);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=302037&siteId=1