Hello I'm new in JavaScript, How can I get the Fruit1
and Fruit2
after I click the button "Click me"
For Example : If I click the button, then choose 2 of fruits from the option. if I were to choose Apple and Banana then it should alert "You choose Apple and Banana"
<html>
<head>
<script type="text/javascript">
function yourChoose() {
alert("Now, let's choose 2 fruit do you like ?");
// code for i choose 2 fruits
var Fruit1 = .....;
var Fruit2 = .....;
alert("You choose "+Fruit1+" and "+Fruit2)
}
</script>
</head>
<body>
<button name="clickme" onclick="yourChoose()" >click me </button><br/>
<a href=""> Apple </a><br/>
<a href=""> Banana </a><br/>
<a href=""> Coconut </a><br/>
<a href=""> Durian </a><br/>
<a href=""> ElderBerry </a>
</body>
</html>
Here is example without jquery but with OOP :)
<html>
<head>
<script type="text/javascript">
function Fruiter(){ // Class that manage your fruits
this.addFruit = function(fr){ // adding fruits
if (this.fr1) {
this.fr2 = fr;
alert("You choose "+this.fr1+" and "+this.fr2); // show messages when all fruits choosed
} else {this.fr1 = fr;}
}
this.chooseFruit = function(fr){ // choosing fruits / reseting
this.fr1 = null;
alert("Now, let's choose 2 fruit do you like ?");
}
}
var f = new Fruiter();
</script>
</head>
<body>
<button name="clickme" onclick="f.chooseFruit()" >click me </button><br/>
<a href="#" onclick="f.addFruit('Apple')"> Apple </a><br/>
<a href="#" onclick="f.addFruit('Banana')"> Banana </a><br/>
<a href="#" onclick="f.addFruit('Coconut')"> Coconut </a><br/>
<a href="#" onclick="f.addFruit('Durian')"> Durian </a><br/>
<a href="#" onclick="f.addFruit('ElderBerry')"> ElderBerry </a>
</body>
</html>
Add the following javascript:
var fruit1=""; //set the global variables
var fruit2="";
function selectFruit(name){
fruit2=fruit1; //this will set the global variables
fruit1=name; //at only 1 point of time 2 fruits are selected!
}
The function selectFruit will make sure that only the latest 2 clicked fruits are stored
Next, change your links to the following:
<a href="javascript:void(0)" onclick="selectFruit('Apple')"> Apple </a><br/>
<a href="javascript:void(0)" onclick="selectFruit('Banana')"> Banana </a><br/>
<a href="javascript:void(0)" onclick="selectFruit('Coconut')"> Coconut </a><br/>
<a href="javascript:void(0)" onclick="selectFruit('Durian')"> Durian </a><br/>
<a href="javascript:void(0)" onclick="selectFruit('ElderBerry')"> ElderBerry </a><br/>
Here's a solution with jQuery : http://jsfiddle.net/guy777/8mJG5/
HTML :
<button id='btn' name="clickme">click me </button><br/>
<a href=""> Apple </a><br/>
<a href=""> Banana </a><br/>
<a href=""> Coconut </a><br/>
<a href=""> Durian </a><br/>
<a href=""> ElderBerry </a>
JavaScript :
$('#btn').on('click', function(e) {
var a = $('a');
var fruits = [];
$.map(a, function(fruit) {
fruits.push(fruit.text);
});
console.log(fruits);
});
U can use this > http://fiddle.jshell.net/gUryq/3/
var fruits = { fruit1 : "", fruit2 : ""};
$("#clickme").click(function() {
if(fruits.fruit1 == "" && fruits.fruit2 == "") {
alert("Now, let's choose 2 fruit do you like ?");
} else {
alert("pls choose the other fruit first!");
}
});
... jquery in use!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.