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

2
  • enclose your function inside $(document).ready() and check once Commented Jun 13, 2013 at 5:22
  • Nope see comment below as you can see from the Plunker & JSFiddle its working without an Anonymous Function Commented Jun 13, 2013 at 8:13

2 Answers 2

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>

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

1 Comment

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
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?

Comments

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.