2

I am trying to add image and button click to jquery dynamically. The image doesn't show. Below is my code.

$("<div/>", { id: "hide" })
    .append('<img src="/Content/Images/alert.png" style="margin-left: auto; margin-right: auto; display: block;" alt="" />')
    .append('<button runat="server" autofocus id="myButton" type="button" style="margin-left: auto; margin-right: auto; display: block;">OK</button>');
$("#myButton").click(function () {
    $("#hide").hide();
});

2 Answers 2

1

You are creating a new element in memory and appending to that but not to the DOM.

Change:

$("<div/>", { id: "hide" }).append...

To:

$("#hide").append...
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried your approach and it doesn't work. Maybe if I can explain what I am doing you can help me better. I need a popup image with some text to show up first thing when my page is loading. Something like a warning, users read it and click ok to dismiss and proceed. I was using session start in Global.asax which was working ok in windows server 2008 R2 but now not working in windows server 2019. I need the popup to come before the DOM loads so basically from the html header
The server shouldn't make a difference since this is client side code run in browser. You can do what you are asking using html and css
Do you have an example where to embed the html? I am using mvc5 _Layout
0

I have finally got it working the way I want. I have created an html page for the redirect. I have also marked some of my controllers with

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

Then in my Glbal.asax Session_Start I check if the Session is not IsReadOnly Redirect to the html page

1 Comment

This isn't an answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.