-1

I'm a novice so this is probably very simple, but it isn’t for me.

<div id="abc" style="width:100px; height:100px; background-color:Blue;" onclick="change()">
  Hi
</div>

And here's a script so that clicking on the box should change the text. But doesn't. Why?

function change() 
{
  var text = "abcde";
  var new1 = "";
  var element = document.getElementById('abc');
  for (var i=0;i<text.length;i++)
    {
      new1 = substring(i,text.length-i);
      element.innerHTML = new1;
    }
}

jsfiddle: here.

1 Answer 1

3

substring() must be applied to a string (the one which you want to extract a part of), so in your case, I believe, you intent something like this:

function change() 
{
  var text = "abcde";
  var new1 = "";
  var element = document.getElementById('abc');
  for (var i=0;i<text.length;i++)
    {
      new1 = text.substring(i,text.length-i-1);
      element.innerHTML = new1;
    }
}

example fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

I think the issue is that the function is not even executing (at least in the Jfiddle). If you put an alert at the top of the function and then click the div, it doesn't work.
@fdsa In his fiddle the JS code is put in the "onLoad" event. Thus it is not known, when parsing the respective div. I changed this to "no wrap (head)", so it works in the fiddle.
Thanks. That was half of the problem. The other half, I saw, was that at the left there's a box where I had onLoad, which I discovered by switching between both jsfiddles. Thanks again.
Because of the order on which JSFiddle executes the different parts the JavaScript doit function is not defined. Try this instead.

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.