Having difficulty with a script not functioning properly within an onclick button

In my script, I am using the following code:

for (var i in $scope.hulls) {
if ($scope.hulls[i].id == 1234) {
console.log($scope.hulls[i]);
$scope.selectedHullShip1 = $scope.hulls[i];
 }
}

The code works fine outside of the onclick button, but fails to run inside the onclick button. I'm struggling to figure out why this is happening as my knowledge is limited and I am learning on the go.

Below is a snippet with the code working outside of the onclick event. Through console logging, I found out that this particular line does not execute properly:

$scope.selectedHullShip1 = $scope.hulls[i];

You can also check the fiddle for further reference here.

var app  = angular.module('myApp',[]);
        app.controller('myCtrl',function ($scope){
          
    $scope.hulls = [
                            {id:4271, "name":"Everest", "src": "Everest", "Class": "Garrison", Cost:{"Oil":200000, "Metal":200000,"Energy":200000,"Zynth":200000,"Time":256075},Vars:{"maxweight":80000, "Combat_Speed":35, "Turn_Speed":35, "Map_Speed":178, "Cargo":20000000, "Repair_Duration":10800, "Mortar_Reload_Decrease":50, "Mortar_Range_Increase":20, "Projectile_Speed_Increase":10, "Splash_Increase":5, "Mortar_Critical_Chance":10, "Building_Damage_Increase":200, "Wall_Damage_Increase":600,               "Armour_Points":240000000          }     },
                            
                            
                            
                            
                            
//Some Random Test Data                         
...

submit.onclick = function(){   
     
     alert("HI")
     



}

      for (var i in $scope.hulls) {
 if ($scope.hulls[i].id == 1234) {
   console.log($scope.hulls[i]);
   $scope.selectedHullShip1 = $scope.hulls[i];
 }
}





          
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
<body> 
    
    <div ng-app="myApp" ng-controller="myCtrl">
    
    
        Kixeye Code: <input id="KixCode" type="text" style="width: 26%" value="eNqrVsoozcnxdFGyMjEyN9RRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmlgbm5rG1AP+nG5g=
">
        <button id="submit">Load</button>  
    
    <br><br>
    
    
    
      
      <select ng-model="selectedHullShip1" ng-options="hull.name group by hull.Class for hull in hulls | orderBy:'name'"></select><br><br>
      
      Output:  {{selectedHullShip1}} <br><br><br><br>
      
  
    
    
    </div>
  </body>

Answer №1

After recognizing my error, I opted to replace the previous button with an ng-click button, resulting in successful functionality.

Initiate

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some effective ways to stretch specific muscles?

I am facing an issue with my select element. The default value is "please choose," but there are options like "Accommodation & Hotels" that are longer and not clearly visible on IE browsers, though they work fine in Firefox. How can I resolve this issue? S ...

Challenges with ColdFusion's floating point calculations

I am encountering an issue with my program where it is not displaying two decimal points properly. For example, when I enter 140.00, it shows as 140.0. Strangely, if I enter 140.15, it displays correctly as 140.15. It seems to consistently drop the zero. B ...

Reduce the amount of ajax calls

Currently, I have implemented checkboxes that serve as search filters on a website. Every time a user checks a filter box, an ajax request is triggered to fetch data from the server and display it. The issue arises when users select multiple checkboxes in ...

Utilizing props in makeStyles for React styling

I have a component that looks like this: const MyComponent = (props) => { const classes = useStyles(props); return ( <div className={classes.divBackground} backgroundImageLink={props.product?.image} sx={{ position: "r ...

Instruction on inserting NativeBase footer into my React Native application?

As someone new to React Native, I am trying to include a footer bar in my app. Below is the code snippet from my dashboard.js file where I attempted to add the footer bar, but it doesn't seem to be working as expected: import React, { memo } from &ap ...

Is there a way to deactivate all dot inputs on number type input in vue.js 2?

Here is an example of my HTML code: <div id="app"> <input type="number" v-model="quantity"/> </div> This is how my Vue component looks: new Vue({ el: '#app', data: { quantity: '' }, watch: { quanti ...

Steps for displaying a particular slide from a thumbnail in a modal with Slick Carousel

My setup includes an array of thumbnails and a Slick Carousel within a hidden modal. Is there a way to trigger the opening of the carousel by clicking on a thumbnail, ensuring that it starts from the corresponding slide? ...

What is the best way to switch between my two images upon clicking?

When I click on an image, it changes to a different image. I achieved this by altering the source of the image upon clicking. Now, my goal is to incorporate a smooth transition between the two images after they are clicked. I attempted to stack the two im ...

Automatically resize ui-select dropdown box to the left side

I've been tasked with incorporating AngularJS ui-select into my project, specifically creating a multiselect dropdown. Currently, the dropdown box automatically adjusts its width based on the length of the longest selectable text, causing it to extend ...

What is the best way to send multiple id values with the same classname as an array to the database via AJAX in Codeigniter?

Hey everyone, I'm facing an issue where I need to send multiple IDs with the same class name but different ID values to the database using AJAX. However, when I try to do this, only the first value is being picked up and not all of them. How can I suc ...

Why does attempting to access an undefined property not trigger an error?

I am curious to know why var myVar = unDef; may trigger a ReferenceError, while var myObj = {}; var myVar = myObj.unDef; runs smoothly? It simply returns undefined without any runtime issues. Despite both not being defined. ...

jQuery UI tabs loaded with Ajax causing Javascript to malfunction

It appears that Javascript is disabled in jQuery UI tabs when loaded through ajax. Testing with jQuery tooltips and .click() alerts shows that Javascript functions properly in tabs not loaded through ajax (IDs present on the page). Below is the method use ...

Troubleshooting a Messaging Error between Background and Another Script

Attempting to transfer selected text from the current page to an HTML page using message passing: content script to background script, then background script to the HTML page. However, encountering errors if the HTML page is not already open, and even gett ...

What is the reason behind observing numerous post requests in the Firebug console after submitting a form through Ajax in jQuery?

I have integrated the jquery Form plugin for form submission and everything seems to be functioning properly. However, upon turning on the firebug console and clicking the submit button, I notice that there are 10 post requests being sent with the same da ...

Tips for achieving seamless scrolling with the combination of javascript and css

Looking for a solution to achieve smooth scrolling on an HTML page without using the #id_name to scroll to a specific div. Instead, I want to scroll to a particular point, such as having a button that scrolls down the webpage smoothly by 250px when press ...

Symphony 5 and Encore: Issue with Bootstrap JS Loading

Encountering issues while trying to integrate Bootstrap's js with Symfony 5, encore, stimulus, etc... All required dependencies like jquery, popper, corejs are installed and functioning, except for Bootstrap's JS components. The compilation of ...

Interact with Circles Through Mouse Movements with d3.js Animation

I am currently utilizing the d3.js library and facing a challenge in meeting the client's requirements. The client has requested for "circles to turn black" and "follow" the mouse when hovered over. I am unsure if d3.js library supports such a featu ...

How can I update getServerSideProps using a change event in Next.js?

Currently, I am faced with the task of updating product data based on different categories. In order to achieve this, I have set up an index page along with two components called Products and Categories. Initially, I retrieve all products using the getServ ...

How do I access a specific child from an object/element retrieved by using the elementFromPoint() method in Javascript?

Is there a method to extract the child element from an element or object retrieved by the function elementFromPoint(x, y); Consider the following scenario: var elem = document.elementFromPoint(x, y); Let's assume that the element returned and saved ...

When attempting to navigate to a different page in Next.js, the Cypress visit functionality may not function as

In my upcoming application, there are two main pages: Login and Cars. On the Cars page, users can click on a specific car to view more details about it. The URL format is as follows: /cars for the general cars page and /cars/car-id for the individual car p ...