2

This has got me stumped the following code:

<!DOCTYPE html>
<html ng-app>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script>
function ctrl($scope){
    $scope.submit = function(e){
        var elem = angular.element(e.srcElement);
        alert($(elem.parent()).html());
        //alert ('first name ' + person.first + ' last name ' + person.last);
    };
}
</script>
</head>
<body>
<div ng-controller="ctrl">
First: <input name="first" value="will" />
Last: <input name="last" value = "kriski" />
    <input type="submit" name="submit" ng-click="submit($event)"/>
</div>
</body>
</html>

Work in jsfiddle and plunker fine (clicking the Submit but displays the HTML in the alert).

but when I run the same code both locally and my personal server the alert reads "undefined"

These both work: http://jsfiddle.net/sjmcpherso/yQs8z/193/ http://plnkr.co/qaqymNFQIe3ziyj7AKXa

The same pasted code on my personal server does not http://sjmcpherson.com/testing/test.html

Really confused

sjm
  • 5,378
  • 1
  • 26
  • 37

2 Answers2

1

Try with this code

<script type = "text/javascript">
    $(document).ready(function(e) {
        function ctrl($scope)
        {
            $scope.submit = function(e){
                var elem = angular.element(e.srcElement);
                alert($(elem.parent()).html());
                //alert ('first name ' + person.first + ' last name ' + person.last);
            };
        }
    });
</script>

or write function ctrl($scope) { //Your code goes here } just before closing the </body>

Harsha Venkataramu
  • 2,887
  • 1
  • 34
  • 58
  • No apply a anonymous function is not necessary, for one the click event runs once the DOM is loaded and two it will stop Angular code from running – sjm Jun 13 '13 at 08:11
1

Doh actually I relized the error was only occuring in Firefox not Chrome and relates to srcElement which isn't compatible with Firefox this post explains a fix How can I make event.srcElement work in Firefox and what does it mean?

Community
  • 1
  • 1
sjm
  • 5,378
  • 1
  • 26
  • 37