I need help with JavaScript

Hi @John_Okoye,

It’s not entirely clear what the problem is from the screenshot you’ve provided, but for a start your code between the script tags should be  alert("myMessage"); and not article. You also need to save your file before you run it (the blue dot in the tab means that you haven’t saved your latest version yet.

Are you getting any error messages in the console when you try to run this code? If so, let us know what they are.

Also, your code within the script tags should automatically be interpreted as JavaScript, without needing to include the attribute type="text/javascript" . But it should still run whether you include it or not, so that won’t be the problem.

In order for us to be able to run any code you are having problems with, and would like us to test for you, you need to include your actual code appropriately formatted, not just a screen shot. Before you add your code to post it here in the forum, click on the </> icon in the menu at the top of the text editor. That will give you 2 sets of 3 back ticks…
```
```
If you now input your code between these, you will end up with it nicely formatted. You will probably still need to organise it a bit more with the correct spacing and indentation etc., but it’s much easier to do that effectively once it’s formatted than when it’s not. It will also help you spot any errors that may have crept in before posting it, as the code is much clearer and easier to read.

Let us know if this hasn’t solved your problem, and if you still can’t get the function alert() to execute.

1 Like

yes, resolved. thank you. The issue was I assumed that JS would populate with ‘alert’ like it did with HTML tags…I didn’t realize that I had to physically type out all the function syntax all out.

I think thereafter it learns though doesn’t it? I’m not sure but this is the playing around that Ivan is talking about which is how I learn best…don’t like to read about it but rather mess around and make mistakes and learn from them.

Thankx again

1 Like

Glad you’ve got it sorted :+1:

We all learn in different ways, and it’s all about finding out which approach works best for you. It is good to experiment with a variety of approaches though — don’t give up on the reading approach just yet; there are loads of great articles and documentation out there! :nerd_face: :wink:

I’m not entirely sure I understand what you mean here… but basically, even though it’s an HTML document, you can write JavaScript syntax within the HTML script tags, and when the browser gets to it, it will execute it as JavaScript, whereas everything before and after the script tags it interprets as HTML. Don’t worry, it took me a while to get my head around what was actually happening when I first started putting HTML and JavaScript together like this :wink: These things become a lot clearer after a bit of time…(and more playing and messing around) :wink:

1 Like

when you start typing the tag in atom it used a predictive completion short of thing, you know when you text someone and are not really looking at it and it sends the wrong word.

well this is the case with atom. it has predictive tags for html but not for JS…I think, I’m a nubie so forgive the incorrect terms…

gotta play more…and force some spinach I guess (reading) as I know that is how you get stronger…

cheers

1 Like

I’ll be totally honest with you, I find the predictive completion thing really annoying so I just ignore it :relaxed: I should probably work out how to turn it off, but…well… you know how these things just get left and put up with :wink:

I’ve just had a little experiment with mine, and I do seem to have some predictive options for JavaScript, but maybe as you say that’s just materialised through learnt behaviour… not sure really… anyway it’s much better for learning if you just ignore it and type everything yourself. As long as you type in valid JavaScript between the HTML script tags it will run no problem.

Enjoy messing around and experimenting! :smiley:

1 Like

Hi I can not make appear the button click function on my website…The code is equal as the Ivan class, unless I mistake. Can someone help?

<html>
<head>
  <title>Great website</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
  <script>

          <button id= "ourButton" >Button text</button>

        <script>
$("#ourButton").click(function(){
  alert.("button clicked!")
});
      </script>

</body>
</html>

1 Like

Hello sir, at the begin of your <body> tag you have an extra <script> tag, removed and your button should appear.

remember that <button> is not a JS native element, is HTML native element, so you should have it outside of the <script> tag.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

2 Likes

Thanks Carlos, it works to show the button. Now, I am trying next exercise to get user input text, however there is something wrong with my script, Could you have a look?

html>
<head>
  <title>Great website</title>
<script> src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>


          <input id="theinput" type= "text" />
<button id="the Button"> Show written text</button>

        <script>
$("#theButton").click(function(){

var textInputted = $("#theInput").val();
alert(textInputted);
});
      </script>

</body>
</html>
1 Like

Hello @Mroman, your ID on <input> tag is: id="theinput", while in your variable textInputted you are pointing to the wrong id "#theInput". the misstype on the i and capital i.

Also your <button> tag had the same error, id="the Button", in your <script> tag you have your function with $("#theButton")....

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

I am on Chapter 19, Dynamic list. I write the same than Ivan but the four fruits do not display. Also the search button do not appear add fruit. I do not know what I am doing wrong…

Blockquote

Great website
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

My favorite fruits

Add fruit
    <script>

$("#addFruitButton").click(function(){
var fruitText = $("#fruitTextInput").val();
alert(fruitText);
});
var fruits = [“Apple”, “Orange”, “Banana”, “Pinapple” ];

var list = $("#fruitlist");

$.each(fruits,function(index,value)¨{

$("

  • ").text(value).appendTo(list);
    });

    console.log(fruits)

      </script>
    
  • Remember you can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.

    
    function formatText(){
    
    let words = “I’m a preformatted Text box, Please use me wisely!”
    
    }
    
    

    prefromatted_text-animated

    Carlos Z.

    1 Like
    <html>
    
    <head>
      <title>Great website</title>
    
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    
    <h1>My favorite fruits</h1>
    <o1 id="fruitlist">
    </o1>
    
    
      <input id="fruitTextInput" placeholder="Write fruit" type= "text" />
    <button id="addFruitButton">Add fruit</button>
    
            <script>
    $("#addFruitButton").click(function(){
      var fruitText = $("#fruitTextInput").val();
      alert(fruitText);
    });
    var fruits = ["Apple", "Orange", "Banana", "Pinapple" ];
    
    var list = $("#fruitlist");
    
    $.each(fruits,function(index,value)¨{
    
      $("<li/>").text(value).appendTo(list);
    });
    
    console.log(fruits)
    
    
          </script>
    
    </body>
    

    Ok sir, you have some few errors in your code.

    Codeline 27: $.each(fruits,function(index,value)¨{... you have a miss-typed " in the function body. Must Delete it.

    Codeline 19: your click function that you use to get the inputed fruit from the <input> tag, you are just binding the value of the input to the variable fruitText, but you should also push it to the array fruits.

    Codeline 27: after adding a new fruit to the array, the list should be refreshed, you could use a function redrawList to do this for you. Take in consideration that you should clear the list before refreshing it.

    Your file is missing the </html> tag at the end.

    Hope you find this useful.
    If you have any more questions, please let us know so we can help you! :slight_smile:

    Carlos Z.

    1 Like
    <html>
    <head>
    <title> This is a website </title>
    </head>
    
    <body>
    <h1> This is the title </h1>
    
    <script>
    var textTodisplay= "helloo";
    document.write("<h2> + textTodisplay + </h2>");
    
    
    
    
    
    
    
    </script>
    </body>
    
    
    </html>
    

    Hi, I dont seem to be able to get the variable to work : I must not be getting something. The display is textTodispaly rather than hello , same for the other exercises. On my first try it did and now exercising it wont.

    Thanks a lot for the help

    Hey @mountainvalue,

    I guesse you made a little mistake in your code. Instead of writing the variable in the string, add it out side it. Your code would look like this below–

    <script>
     var textTodisplay= "helloo"; 
     document.write("<h2>" + textTodisplay + "</h2>"); //the variable is outside the string 
    </script>
    

    This will solve your problem. :slight_smile:

    Happy Learning! :slight_smile:

    1 Like

    Malik,
    Thanks a lot , but I don’t get it . At all … I basically copied the video and it worked once…there must be a false input somewhere …

    Hi @mountainvalue,
    If you type the variable between the double quotes, it is considered as a string and not a variable.
    For it to recognise it as a variable, it needs to be outside the double quotes. That’s what I did to your peice of code.

    previously --
     document.write("<h2> + textTodisplay + </h2>");
    
    now -- 
     document.write("<h2>" + textTodisplay + "</h2>");
    

    Let me know if it’s still not clear.

    Happy Learning! :slight_smile:

    1 Like

    got it thank a thousand

    Hello, I am looking for help. So, in the lesson ABSTRACTION and returning values from functions from Javascript course, you did a code for multiplication but I can´t figure out the logic.


    I just have a few doubts:

    1.- How are you telling toReturn to add “b” times?
    2.- How does the computer knows that the loop For with variable counter means that any value of toReturn means adding that by any “b” times?.

    I know is a simple example and a dumb one but I really want to understand how, hope someone answers, thanks!. @ivan

    Think the problem with that is that you have spelt ‘length’ incorrectly. So you are not getting the length of the string at all. If you correct it should work.

    1 Like