(using jQuery)
1) I have an on click javascript that creates a form with a pulldown and a div, like so:
$('#closestDiv').on('click','.firstClick', function(event){
var pass = $(this).attr('data-pass');
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('id',"newForm");
f.setAttribute('action',"SubmitForm.php");
var d = document.createElement("select");
var op1 = new Option();
op1.value = "1";
op1.text = "1";
op1.setAttribute('class', "secondClick");
op1.setAttribute('data-pass', pass);
d.options.add(op1);
f.appendChild(d);
var div = document.createElement("div");
div.setAttribute('id', "newDiv");
div.innerHTML = 'REPLACE CONTENT';
f.appendChild(div);
$("#closestDiv").append(f);
}); // end on click
2) I then have an onclick for the pulldown options that load Content into the said div via ajax.
$('#closestDiv').on('click', '.secondClick', function () {
$(this).prop('selected', true);
var value = $(this).attr('value');
var pass = $(this).attr('data-pass');
$.ajax({
url: 'getContent.php',
type: "get",
data : {value: value, pass: pass},
success: function(response){
var result = JSON.parse(response);
$('#newDiv').html(result['newContent']);
console.log(result['newContent']);
console.log($('#newDiv'));
} // end success function
}); // end ajax call
}); // end on click
The console shows correct newContent being passed, and in console the div element shows correct html with newContent, but the actual page div is still showing "REPLACE CONTENT" (ie not updating).
I've done this successfully when the dropdown is loaded with the page rather than via javascript. I am wondering if I'm not understanding the order in which DOM loads things, and how to make this work. For some reason I thought that if I built the dropdown in javascript (rather than via ajax - which pretty much gives identical result and which I much prefer if the fix is the same...) it would become functional right away.
I've also noticed the dropdown doesn't show what's been selected, even though the correct selected option is getting passed to ajax.
Any pointers most appreciated. Thanks!