Jump to content
vmars316

Trouble with alert placement ?

Recommended Posts

Hello & Thanks :

Here is my code:

	<script id="js" contenteditable >
function createArticleHeader(text= "Article Heading") {
  var p_ah = document.createElement('p');
    alert{"p_ah"};
  p_ah.innerHTML = text;
  p_ah.setAttribute('contenteditable', 'true');
  var content = document.getElementById('content');
  content.appendChild(p_ah);
}
createArticleHeader("Article Header");
</script>

This code runs great, without the alert .

But when I add the alert{} line , it doesn't run at all .

Is alert{} in the wrong place or what ?

Thanks for your help .

 

Share this post


Link to post
Share on other sites

alert() is a function, it should be using brackets (), not curly braces{}.

This is a syntax error: createArticleHeader(text= "Article Heading"). Javascript is not like PHP, you cannot give default values to the function arguments (unless this is an ECMAScript 6 or 7 feature I wasn't aware of, I still wouldn't recommend using it for compatibility reasons).

Share this post


Link to post
Share on other sites
7 minutes ago, Ingolme said:

This is a syntax error: createArticleHeader(text= "Article Heading"). Javascript is not like PHP, you cannot give default values to the function arguments (unless this is an ECMAScript 6 or 7 feature I wasn't aware of, I still wouldn't recommend using it for compatibility reasons).

Heck sorry about that, that was my advice. Just checked MDN, its a feature of ES2015. Only Internet Explorer doesn't seem to be supported by it. (Had no idea it was 'new')

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters

Share this post


Link to post
Share on other sites

Thanks;

I thought I would try global variables .


<script type='text/javascript' > 
    var  NewLink = "New Link"
    var  TextParagraph = "Text Paragraph"
    var  ArticleHeading = "Article Heading"
</script>

	<script id="js" contenteditable >
function createNewLink() {
          var li_nl = document.createElement("li");
          li_nl.innerHTML = NewLink;
		  alert(' function createNewLink = ' + NewLink);  
          li_nl.setAttribute("contenteditable", "true");
          var nav = document.getElementById("nav"); 
          nav.appendChild(li_nl);
        }
		</script>	

The Alert works fine .

 Thanks

Share this post


Link to post
Share on other sites

Hello & Thanks ;

I did read  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters 

But it was too much info , I couldn't understand it  . 

I have heard that using global variables is bad practice .

Is that true here ? 

It all runs fine .

I read thru w3Schools  js css html  Tutorials years ago .

So yesterday & today , i read thru this series of Tutorials :

 https://www.digitalocean.com/community/tutorial_series/understanding-the-dom-document-object-model 

I highly recommend it to newbies or oldies like me .

Thanks All

 

Edited by vmars316

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...