Dano Dano - 1 year ago 127
jQuery Question

.val has no effect, and also .append has no effect

My code:

$.ajax({
url: "{{URL::to('/announcement/get/fields')}}",
type: "post",
data: {id:id},
success: function (response) {

this.title = "n/a";
this.body = "n/a";

var self = this;

$.each( response, function( key, value ) {
self.title = response['aTitle'];
self.body = response['aBody'];
});

$('#announcementEditTitle').val(this.title);
$('#announcementEditBody').append(this.body);
// CKEDITOR.instances[editEditor].insertText(this.body);
console.log(this.title);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}


});


response returns an array like this:

aTitle: value

aBody: announcement content


There is a
SELECT
option in the form and once the user clicks on an announcement title, the values of that announcement should be inserted into the announcement title field and announcement body.

Value was working for few moments but somehow I broke it, and append wasn't working at all.

For the body
#announcementEditBody
I have a
textarea
with CKeditor on it.

if I do
<textarea>Some text</textarea>
"Some text" will show in the
ckeditor
but if I append it with jquery nothing will show.

UPDATE



$('#announcementEditTitle')
returns length 0, but
this.title
returns the length of the title so the variable isn't empty

UPDATE 2



I just noticed that I did not need the .each, my bad, so I did this

$("#announcementEditTitle").val(response['aTitle']);
$('#announcementEditBody').append(response['aBody']);


But it still doesn't work, yet if I do
console.log(response['aTitle']);
it works.

Fixed the title, but the body still doesn't work.

Answer Source

You

  • don't need the each()
  • probably targeted #announcementEditTitle wrongly (maybe not an ID but a class? spelling?)
  • are using the wrong jQuery method to fill the Body, use val (or try text/html, but it should be val)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download