the lambda function how to visit the external array - javascript

<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js
"></script>
<script type="text/javascript">
$(function(){
for(i=0;i<5;i++){
var ids=[1,2,3,4,5];
$("#btn"+ids[i]).click(function(){
alert("btn"+ids[i]);
});
}
});
</script>
</head>
<body>
<button id="btn1">按钮1</button>
<button id="btn2">按钮2</button>
<button id="btn3">按钮3</button>
<button id="btn4">按钮4</button>
<button id="btn5">按钮5</button>
</body>
</html>
Clicking the button should alert the value in the array, but it instead alerts btnundefine.
How can I visit the value in the external array in the lambda function?

Define i with let. Otherwise, it will try to access ids[5] and prints undefined because ids array only has 5 elements.
$(function(){
var ids=[1,2,3,4,5];
for(let i=0;i<5;i++){
$("#btn"+ids[i]).click(function(){
alert("btn"+ids[i]);
});
}
});
<script typetype=="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<button id="btn1">按钮1</button>
<button id="btn2">按钮2</button>
<button id="btn3">按钮3</button>
<button id="btn4">按钮4</button>
<button id="btn5">按钮5</button>

This is workable also:
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js
"></script>
<script type="text/javascript">
$(function(){
var ids=[1,2,3,4,5];
for(i=0;i<5;i++){
console.log("#btn"+ids[i]);
$("#btn"+ids[i]).click(function(){
alert(this.id);
});
}
});
</script>
</head>
<body>
<button id="btn1">按钮1</button>
<button id="btn2">按钮2</button>
<button id="btn3">按钮3</button>
<button id="btn4">按钮4</button>
<button id="btn5">按钮5</button>
</body>
</html>

Related

Difference between ':button' and 'button' as selector

<!DOCTYPE html>
<html lang="en">
<head>
<title>
</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/custom.css" />
</head>
<body>
<button value="">Button 1</button>
<button value="">Button 2</button>
<button value="">Button 3</button>
<script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
<script type="text/javascript" src="js/custom.js" ></script>
</body>
</html>
Below is code in custom.js
$(':button').on('click',function(){
alert('Hello');
});
and when I changed the code in custom.js to
$('button').on('click',function(){
alert('Hello');
});
They do the same work that is display alert on click,But I want to know the difference between 'button' and ':button'
Using only button will select only <button></button> elements, while :button will select <button></button> and <input type="button" />
See https://api.jquery.com/button-selector/ for more information.
button only selects the button elements, whereas :button also selects the input type="button"
$(':button') selects <button> tags or <input /> tags with type="button"
$('button') selects only <button> tags
$(':button').on('click',function(){
alert('You clicked a tag of type: ' + $(this).prop('tagName'));
});
$('button').on('click',function(){
alert('Hello from ' + ($(this).html() || $(this).val()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button value="">Button 1</button>
<button value="">Button 2</button>
<button value="">Button 3</button>
<input type="button" value="Input Button" />
$(":button") : selects both "button" tag and "input type=button" as well
while $("button") : only selects

javascript jquery .show() not working

For some reason .hide works, but .show not?
This is my code:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<button onclick="alertShowAccount()">Add account</button>
<script>
function alertHideAccount() {
$("#addAccount").hide();
}
function alertShowAccount() {
$("#addAccount").show();
}
</script>
<div style="display:none" id="addAccount">
<button class="btn btn-md" onclick="alertHideAccount()">Cancel</button>
</div>
<body>
Anyone has an idea why it is not working? The function IS being called, I tested it with an alert. Also the console gives no errors.
Here's how I did man, it worked for me this way...
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
$('#addAccount').hide();
$('#hide_div').on('click',function(){
$("#addAccount").hide();
});
$('#show_div').on('click',function(){
$("#addAccount").show();
});
});
</script>
<body>
<button id="show_div">Add account</button>
<div id="addAccount">
<button class="btn btn-md" id='hide_div'>Cancel</button>
</div>
<body>
So here's what happened, we changed Onclick to just the ID of the element, and called it on the script with $(elem).on('event',function());

individual Div with ID not hiding through Jquery inside form tag

I have multiple divs inside a form tag.
<form id="RegdForm">
#Html.Partial("../Partials/_PatientEdit")
The partial view,
<div id="zafar"> <h3>Zafar</h3> </div>
When I try to hide all the divs its working like below
<script type="text/javascript">
$('Div').hide();
</script>
But when I try to hide specific Div it's not hiding at all.
<script type="text/javascript">
$('#zafar').hide();
</script>
I am using ASP.Net MVC 5 with Razor.
Any help appreciated.
Try this
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style type="text/css">
</style>
</head>
<body>
<div id="zafar">
<h3>Zafar</h3>
</div>
<div class="well well-sm">
<button class="btn btn-success" type="submit">Save</button>
<button class="btn btn-primary" type="button" data-bind="click: printPatient">Print</button>
<button class="btn btn-danger" type="button" data-bind="click: resetForm">Reset</button>
</div>
<button id="hide">Hide</button>
<button id="hideAll">Hide All</button>
<script type="text/javascript">
$(document).ready(function(){
$("#hide").click(function(){
$('#zafar').hide();
});
$("#hideAll").click(function(){
$('Div').hide();
});
});
</script>
</body>
</html>
Here is your answer:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#zafar").hide();
});
</script>
Add your respective function inside this.
$(function() {
$('#zafar').hide();
})
After a long googling I found out one solution to this.
As the div was inside the form tag, the way accesing the div can be done as,
<script type="text/javascript">
$('#RegdForm #zafar').hide();
It helped me to achieve the task.

How to get html output from javascript/jQuery?

I have a javascript from textarea:
<textarea id="result">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="widget.js"></script>
<script type="text/javascript" src="test.php"></script>
</textarea>
I want to get html from this javascript:
<script>
$(document).ready(function(){
$(".submit").click(function(){
var html = $('#result').val();
$('#content').html(html);
});
});
</script>
<input type="button" class="submit" value="Ok" />
<div id="content"></div>
Result output is an error. How do I do this?
use var html = $('#result').html();
demo http://jsfiddle.net/gFZ73/2/

What is wrong with selecting with my drop down element

<html>
<head>
<script type='text/javascript' language='javascript' src="http://localhost/javascript/jquery-1.4.2.js">
</script>
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$("#button").mousedown(function(){
dropDownMenu = $("#dropDownMenu");
alert(dropDownMenu.options[0].text);
});
});
</script>
</head>
<body>
<select id="dropDownMenu"><option>Test</option></select><br>
<input id="button" type="button">
</body>
</html>
Try using the text() function:
$("#button").mousedown(function() {
var selectedItemText = $('#dropDownMenu :selected').text();
alert(selectedItemText);
});
Your code dropDownMenu = $("#dropDownMenu");
alert(dropDownMenu.options[0].text);
Here dropDownMenu is a JQuery object. So dropDownMenu.options is not defined. Use dropDownMenu[0] or dropDownMenu.get(0) to get the first DOM element which is a
<select>...</select>

Categories

Resources