1

i can't seem to make the old js scripts that i found on the magical SO.

<div class="form-actions">
    <%= f.button :submit, class:"btn btn-success create-campaign" %>

  </div>

this is my submit button.But there is a problem.

<div class="message-div" id="textField" contenteditable="true" maxlength="180"></div> 
<%= f.input :message ,as: :text,as: :hidden  ,input_html: { class: 'special', maxlength:'180' } %>

i use div to get the input for what i have done for it but my form takes its input from the hidden text area.

So i tried to do take take the content of the div and write it to the text area when submit button clicked.

but the js script that i wrote does not getting executed.

$(document).on("click",".create-campaign",function(e){

console.log('asdasd');

var a = $('.message-div').html();


document.getElementById('.special').value = a;

});

this is the code.

When i changed the listener class to another button, it works without a problem ( at least i see console log) but not when the submit button is pressed.

What should i do?

Cheers.

2
  • where is myTextarea class in input field? Commented Aug 26, 2014 at 13:10
  • @urjitonrails yeah sorry i copied it from ctrl-z guess i did too much.I'll edit the question but thats not the problem since the code doesn't even come to the console part. Commented Aug 26, 2014 at 13:12

1 Answer 1

2

You need to prevent the default submit action from being triggered:

$(".create-campaign").click(function(e) {
  e.preventDefault();
  console.info('asdasd');
  var a = $('.message-div').html();
  alert(a); // just to check that your data is there
  document.getElementById('.special').value = a;
  $("#YOUR_FORM_ID").submit();
});
Sign up to request clarification or add additional context in comments.

3 Comments

just tried it, didn't work, it didnt prevent the submit.I called it in my view between js script tags
Are you wrapping your JS with 'jQuery(document).ready(function() {});' ? You should also move this into some asset file under 'app/assets/javascripts/' and not directly include this in your view.
It might be because you are calling getElementById with a class selected ".special" instead of calling getElementsByClassName("special")[0].value or $(".special").val()

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.