0

So, i tried changing a p element by calling function through getElementById(). But when i click the button nothing happens.

when i click the button it should change car brands to the length of the string and and also add lamborghini.

Here's the code:-

<html>
 <head>
   <script src="javascript_file.js" type="text/javascript"></script>
 </head>
  <body>

  <h1>strings and functions</h1>
  <p id="pFunction">hi</p>
  <button type="button" onclick="document.getElementById('pFunction').innerHTML = 'bye'">click!</button>

  <p id="slice">car brands</p>
  <button onclick="sliceF">Click it!</button>
 </body>

</html>

Js:-

function sliceF(){
   var cars = "bmw , lamborghini , toyota";
   var lengthCars = cars.length;
   var sliceCars = cars.slice(6 , 16);
   document.getElementById('slice').innerHTML = "the length of the string is " + lengthCars + "<br/>" + "the brand in the middle is " + sliceCars;
}
Subhra
  • 1
  • 1
    [Don't use inline handlers](https://stackoverflow.com/a/59539045), they have a demented scope chain, require global pollution, and have quote escaping issues. Use `addEventListener` instead – CertainPerformance Apr 15 '20 at 06:39
  • You can add this on a fiddle, it will be easier to answer: https://jsfiddle.net/ – Ivan Derlich Apr 15 '20 at 06:59

3 Answers3

0

change "sliceF" to "sliceF()"

is it working now.?reply if not working..

user12449933
  • 170
  • 1
  • 6
-1

you have forgott the brackets for function call:

<html>
 <head>
   <script src="javascript_file.js" type="text/javascript"></script>
 </head>
  <body>

  <h1>strings and functions</h1>
  <p id="pFunction">hi</p>
  <button type="button" onclick="document.getElementById('pFunction').innerHTML = 'bye'">click!</button>

  <p id="slice">car brands</p>
  <button onclick="sliceF()">Click it!</button>
 </body>

</html>

https://jsfiddle.net/

chriss
  • 320
  • 2
  • 15
  • don't understand that bad. Do not thank, but vote and mark as a solution: Ask questions, get answers, no distractions – chriss Apr 15 '20 at 07:01
-1

I think you just need to change

   onclick="sliceF"

to

   onclick="sliceF()"
s.blnc
  • 76
  • 6