Sorry about the ambiguous title but I couldn't think of anything better.
I've been struggling with this issue for a few days and haven't been able to find a way around it. A little background about my code: I have a textbox savedInfo and it stores values from different sessions. So if savedInfo is empty, I'd like to confront the user with another textbox. Once the user inputs info into the textbox and presses enter then the .val() of the textbox goes to savedInfo and the textbox is replaced with a label that has 2 buttons that appear when hovered. These two buttons give the user the ability to remove or save the textbox.
The problem I am having is that my code doesn't seem to flow correctly. For instance, I have an if statement that checks if savedInfo is empty and if it isn't then show the label text of savedInfo. Then INSIDE the keypress function I check if one of the hover buttons is pressed but the code doesn't work since it is inside the keypress function. If I take it out of the keypress function then it doesn't work for when the keypress label appears.
Probably best if I explain with pseudo code:
$(document).ready(function() {
if($("savedInfo").val() != "") {
someDiv..innerHTML(<label>$("savedInfo").val() + hover shows buttons</label>);
} else {
//Create textbox id `textbox`
}
$(function() {
$("#textbox").keypress(function(e) {
if (e.keyCode == 13) {
//set innerHTML to a label with text of `textbox`.
//Check if button#1 is pressed. If yes, then do something
//Check if button#2 is pressed. If yes, then do something
}
});
});
});
So the button#1 and button#2 ids are being created inside the if but don't work inside the if because they are inside the keypress.
I hope this wasn't too confusing. Any ideas on how to make this work better? Thanks
keypressevent of dynamically generated button... correct?.on()on parent element that does exist and provide a selector. For example,$(PARENT_ELEMENT).on(EVENT, CREATED_ELEMENT_SELECTOR, CALLBACK)