Is it possible to assign HTML text within an element to a JavaScript variable? After much Googling, I note that you can assign HTML elements to a variable, but I want the actual text itself.
Details about my goal: I am currently working on a CRUD application, and with the click of a delete button, a modal will display and ask the user for confirmation before deleting the record. Once the button has been clicked, I want to retrieve HTML text within a specific element used for AJAX call data. However, what I have tried so far is not being logged to the console; even when I change the global variable to var deleteLocationID = "test"; I doubt the modal displaying will affect the click function?
The code:
var deleteLocationID;
$("#deleteLocationBtn").click(function () {
deleteLocationID = $(document).find(".locationID").val();
console.log(deleteLocationID);
});
What I have tried so far: Changing "deleteLocationID = $(document).find(".locationID").val();" to the following variations:
- deleteLocationID = $(document).find(".locationID").html();
- deleteLocationID = $(".locationID").val() / deleteLocationID = $(".locationID").html();
- deleteLocationID = document.getElementsByClassName("locationID").value;
Any help would be much appreciated.