Jump to content

Pravin Kumar Raja

Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by Pravin Kumar Raja

  1. Thanks a lot. That helped!

    Pravin Kumar.

    <!DOCTYPE html>
    <html>
    	<head>
    		<script>
    			let getID = (x) => x.id;
    			let getText = (x) => x.value;
    			function Click(x) {
    				input = Array.from(document.getElementsByTagName("input"));
    				alert(input.map((x.id == "id") ? getID : getText));
    			}
    		</script>
    	</head>
    	<body>
    		<input type="input" id="input1"> </input>
    		<input type="input" class="input" id="input2"> </input>
    		<button id="id" class="input" onclick="Click(this)"> ID </button>
    		<button id="text" onclick="Click(this)"> Text </button>
    		<script>
    			input = document.getElementsByClassName("input");
    		</script>
    	</body>
    </html>

     

  2. I was using map function in my code but it simply do not work! I must be doing some silly mistake but I am not able to find it out.

    Can you Gurus help?

    Here is the code:

    <!DOCTYPE html>
    <html>
    	<head>
    		<script>
    			getID = (x) => x.id;
    			getText = (x) => x.value;
    			function Click(x) {
    				input = document.getElementsByTagName("input");
    				alert(Array.from(input.map((x.id == "id") ? getID : getText)));
    			}
    		</script>
    	</head>
    	<body>
    		<input type="input" id="input1"> </input>
    		<input type="input" id="input2"> </input>
    		<button id="id" onclick="Click(this)"> ID </button>
    		<button id="text" onclick="Click(this)"> Text </button>
    	</body>
    </html>

    What I want it done is like this: When I click button "ID", list of IDs of the input field should be displayed. Likewise, when I click button "Text", it should display the list of texts which the input fields have currently. The functions getID and getText extracts the ID or text respectively when the argument is a single input field.

    Regards,

    Pravin Kumar.

  3. This is in connection with the web page https://www.w3schools.com/js/js_arrays.asp.

    Array.isArray function does not return correct true when its argument is an array of object. What can be the reason? Is it a bug?

    A method suggested by W3ishools to use a tailor-made function

    function isArray(x) {
      return x.constructor.toString().indexOf("Array") > -1;
    }

    which also does not work, as in the code given below:

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript Arrays</h2>
    
    <p>This "home made" isArray() function returns true when used on an array:</p>
    
    <p id="demo"></p>
    
    <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    document.getElementById("demo").innerHTML = 
    	isArray(document.getElementsByTagName("p"));
    
    function isArray(myArray) {
      return myArray.constructor.toString().indexOf("Array") > -1;
    }
    </script>
    
    </body>
    </html>

    The function returns false.

    What is a foolproof method to check whether a variable is an array?

  4. I had 2 INPUT boxes and one BUTTON. I want to take the control back to the current INPUT after BUTTON is clicked. That part is working fine with the HTML file shown below:

    <!DOCTYPE html>
    
    <html>
    	<head>
    		<script>
    			var last;
    			function Focus(input) {
    				last = input;
    			}
    			function Blur(input) {
    			}
    			function Click(button) {
    				if (typeof last != "undefined")
    					last.focus();
    			}
    		</script>
    	</head>
    	<body>
    		<input id="I1" onfocus="Focus(this)" onblur="Blur(this)"> </input>
    		<input id="I2" onfocus="Focus(this)" onblur="Blur(this)"> </input>
    		<button id="B" onclick="Click(this)"> Click </button>
    		<script>
    		</script>
    	</body>
    </html>

    But I wanted to code onblur function Blur to incorporate check whether the input is exited with a blank string and to return control to the blank INPUT field after displaying an alert to that effect. This displays the alert box continuously. Can you suggest how this can be avoided. Here is my code:

     

    			function Focus(input) {
    				if (typeof last != "undefined")
    					if (last.value.length == 0)
    						alert("Blank!");
    				last = input;
    			}

     

×
×
  • Create New...