6 Replies - 874 Views - Last Post: 10 October 2014 - 08:38 AM

#1 ctincristi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-October 14

Code that works on Fiddle but doesn't work on local

Posted 09 October 2014 - 04:20 PM

Hi,

I have this code: jsfiddle.net/k3b7uvhz/2/

It works on fiddle but on local it does not work. What should be the problem?

Fiddle:

$('#addnewline').on('click', function (e) {
    e.preventDefault();
    var $textarea = $('#thetext');
    $textarea.val(function () {
        return $(this).val().substring(0, this.selectionstart) + "<br>" + $(this).val().substring(this.selectionstart);
    });

});

<button id="addnewline">New line</button>
<br />
<textarea rows="4" cols="50" id="thetext">Some text</textarea>



My version on localhost:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin zone</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$('#new_line').on('click', function(e){
    e.preventDefault();
    var $textarea = $('#description');
    $textarea.val(function(){
        return $(this).val().substring(0, this.selectionstart) + "<br>" + $(this).val().substring(this.selectionstart);
    });
    
});
</script>
</head>

<body>
<button type="button" id="new_line">New Line - BR</button><br>
 <textarea name="description" id="description" cols="100" rows="20"></textarea>

</body>
</html>



Whats the catch?

Is This A Good Question/Topic? 0
  • +

Replies To: Code that works on Fiddle but doesn't work on local

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,319
  • Joined: 12-December 12

Re: Code that works on Fiddle but doesn't work on local

Posted 09 October 2014 - 04:23 PM

Java and Javascript are completely different languages.

There is also a jQuery sub-forum. Topic moved.
Was This Post Helpful? 0
  • +
  • -

#3 ctincristi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-October 14

Re: Code that works on Fiddle but doesn't work on local

Posted 09 October 2014 - 04:29 PM

OK, but any solution to my question please?! Sorry, I didn't know where to post the topic.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3044
  • View blog
  • Posts: 11,699
  • Joined: 03-December 12

Re: Code that works on Fiddle but doesn't work on local

Posted 09 October 2014 - 06:37 PM

Have you checked your browser console to see what errors are present?
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3044
  • View blog
  • Posts: 11,699
  • Joined: 03-December 12

Re: Code that works on Fiddle but doesn't work on local

Posted 09 October 2014 - 06:45 PM

Place the Javascript just before the </body> tag. The code you produced has it in the head and will not have the elements you are trying to work with. You would use,

$.ready( function() {
   // Place code here
});


But, it is generally a better idea to do the above.
Was This Post Helpful? 0
  • +
  • -

#6 ctincristi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-October 14

Re: Code that works on Fiddle but doesn't work on local

Posted 10 October 2014 - 01:22 AM

YES YES YES, totally worked in this way!! Thank you so much!!
Was This Post Helpful? 0
  • +
  • -

#7 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

Re: Code that works on Fiddle but doesn't work on local

Posted 10 October 2014 - 08:38 AM

The reason this is happening is because you are trying to add en event listener before the element is created.

It is good practice to add your code below the html just above the ending body tag to prevent this type of problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1