The following is my ajax
$.ajax({
url: formUrl,
complete :function(){
$("#formContainer").removeClass("some-class");
},
success: function(response){
$("#formContainer").html(response);
initFunctionOne();
initFunctionTwo();
initFunctionThree();
}
});
}
‘response’ contains respective html forms in string format depending on what screen is being loaded. Is there any way i can get the form ID from that response so that I can use switch case statements to only call the function whose form ID matches with the form ID in the html response.
I’m initialising the three functions outside document.getready and the above mentioned ajax call is inside the document.getready
I’ve tried to use .find and .filter but both of them did not work due to an error (error : .filter is not a function)
3
Answers
Not sure if this is a good practice but i have declared a variable and set the substring of the form ID using indices. And using that variable i have used if else conditions to call the functions
As your response string is in HTML format, you can get the form id in a nice, easy and clean way by making use of DOMParser as below:
Alternatively, a more detailed and reusable version is given below with an example:
If you want to test the above, you can just copy/paste this code snippet in a
<script>...</script>
block to a blank HTML file ‘example.html’ and check the Console logs after you open this in your browser. This should just work fine as above.Try this to get the id of from in the response HTML.