0

thanks in advance to all. I really don't know where to start my question's and whats the right terms for what I'm looking for, but sure you guys have some answers for a newbie. I'm trying to get better with js and I want to create a simple push notification msg that will show some pre-written text in the html markup. I'v achieved that with some functions manipulating the css but not sure if its a good method of doing so.

// get msg div
var msg = document.getElementById('msg');
// get button
var btn = document.getElementById('btn');
// get test button
var closeBtn = document.getElementById('closeBtn');

// event lisnters - click btn to run msg
btn.addEventListener('click',runMsg);
// event lisnters - click btn to close msg
closeBtn.addEventListener('click',closeMsg);

// functions list
function runMsg(){
  msg.style.display = 'block'
}

function closeMsg(e){
  if(e.target === closeBtn){
    msg.style.display = 'none';

  }
}

1.method one: what is a better way of doing that? creating the html markup and all the css premade and in position just hidden and using js to reveal it?

2.method two: creating all of the html markup using js when a user clicks a certain element using the createElemnt() method ?

**I'm just curious how all the plugins out there create this, my guess is that they have the markup ready and they just linked the two together in some way.

hoping I was clear, cheers to all.

3
  • 2
    You have tagged jquery here , but i dont see that you are using any jquery methods ? Commented Jan 19, 2018 at 12:27
  • 1
    You can either build the html in the js or use pre-built html - there's no real difference. The choice would likely be based on what tech you are using to build the html (eg c# MVC partial views) and, most importantly, how complicated that html will be. Don't use 100s of strings to build html, it's unmaintainable. The easiest method is to pre-build the html and use a simple .show(). Commented Jan 19, 2018 at 12:32
  • hey thanks for the help, I'v tried messing with that to create that feature in a running word press site / maybe in future create a simple plugin for practice. so what your saying is that in most cases the element is already in the position but hidden and js only shows it? Commented Jan 19, 2018 at 12:42

3 Answers 3

1

Since you have tagged Jquery and If you are using Jquery you can simply achieve what you are doing by this,

$("#btn").click(function(){ //Capture btn click
    $('#msg').css('display','block') // display msg
}); 

$("#closeBtn").click(function(){ // Capture closeBtn click
    $('#msg').css('display','none') // hide msg
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

Why would use you .css("display", "block") instead of .show() and .hide() ?
Yes, you could use that, i just showed how to manipulate css through Jquery :)
1

Sounds like you are looking for a progressive web app. Start your journey here.

1 Comment

maybe I am I really not sure, but I'l check what you suggested cheers,Thanks man for the help appreciate :)
1

The more elements you have pre-generated, and hidden, the slower your page will be. Also, even if you build the entire page with all potential pop-up not all users will be using/triggering them, thus it is a waste. That is why the consensus in web development is that you only build elements when you need them.

In some cases where you are targeting mobile devices you might want to prepare all assets ahead of time but you need to read up on https://developers.google.com/web/progressive-web-apps/ and potentially https://ionicframework.com/

2 Comments

hey thanks bro, yeah that was one I asked my self. so basically hidden elements are taking space. so if I'm creating the element only when a user clicks is that better? like mentioned in Question number 2? plus Just for my knowledge how does all the plugins make this happen ?
if you look in the Angular documentation you will notice that all the code is there but only what you see (and everything attached to it) is generated - a process called injection. Everything else is done on the fly - i.e. the beauty of js client-side rendering: angular.io/guide/architecture

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.