I have used 3 functions. getvalyear() and getvaltrimester() are used to get value of selected dropdown. getVal() gets id as parameter from the clicked button.
function getvalyear(ctrl1){
var sel = ctrl1.selectedIndex;
var selVal1 = ctrl1.options[sel].value;
}
function getvaltrimester(ctrl2){
var sel = ctrl2.selectedIndex;
var selVal2 = ctrl2.options[sel].value;
}
function getVal(clicked_id)
{
}
getvalyear() and getvaltrimester() function get's called from select menu as
Select Year:
<select name="year" id="year" style="width: 200px" onchange="getvalyear(this)">
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
Select Trimester:
<select name="trimester" id="trimester" style="width: 200px" onchange="getvaltrimester(this)">
<option value="1st">1st</option>
<option value="2nd">2nd</option>
<option value="3rd">3rd</option>
</select>
getVal() function get's called on button click as
<?php
require('connect.php');
$sql = "SELECT ictv_id,ictv_name, lb_name FROM ictv_details where region_id='1' and lb_type='DDC' ";
$result = mysql_query($sql)or die(mysql_error());
echo "<div style='text-align: left'>";
echo "<table>";
echo "<tr><th>Name</th><th>Local Body</th><th>Absenteeism</th>
<th>Creativity</th><th>Problem Solving</th><th>Submit</th></tr>";
while($row = mysql_fetch_array($result)){
$ict_id= $row['ictv_id'];
$name= $row['ictv_name'];
$lb= $row['lb_name'];
echo "<tr><form name=\"form$ict_id\" id=\"form$ict_id\"><td>".$name."</td><td>".$lb."</td>
<td>
<select name=\"absent$ict_id\" id=\"absent$ict_id\" style=\"width: 200px\">
<option value></option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>
<td>
<select name=\"creative$ict_id\" id=\"creative$ict_id\" style=\"width: 200px\">
<option value></option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>
<td>
<select name=\"problem$ict_id\" id=\"problem$ict_id\" style=\"width: 200px\">
<option value></option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>
<td>
<input type=\"button\" name=\"submit$ict_id\" id=\"submit$ict_id\" value=\"Submit\" class=\"btn btn-primary \" onclick=\"getVal(this.id)\">
</td>
</form>
</tr>";
}
echo "</table><br /><br /></div>";
// mysql_close();
?>
My query is how can I use Value of selVal1 and selval2 in getVal function without passing argument.
I have used 3 functions. getvalyear() and getvaltrimester() are used to get value of selected dropdown. getVal() gets id as parameter from the clicked button.
function getvalyear(ctrl1){
var sel = ctrl1.selectedIndex;
var selVal1 = ctrl1.options[sel].value;
}
function getvaltrimester(ctrl2){
var sel = ctrl2.selectedIndex;
var selVal2 = ctrl2.options[sel].value;
}
function getVal(clicked_id)
{
}
getvalyear() and getvaltrimester() function get's called from select menu as
Select Year:
<select name="year" id="year" style="width: 200px" onchange="getvalyear(this)">
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
Select Trimester:
<select name="trimester" id="trimester" style="width: 200px" onchange="getvaltrimester(this)">
<option value="1st">1st</option>
<option value="2nd">2nd</option>
<option value="3rd">3rd</option>
</select>
getVal() function get's called on button click as
<?php
require('connect.php');
$sql = "SELECT ictv_id,ictv_name, lb_name FROM ictv_details where region_id='1' and lb_type='DDC' ";
$result = mysql_query($sql)or die(mysql_error());
echo "<div style='text-align: left'>";
echo "<table>";
echo "<tr><th>Name</th><th>Local Body</th><th>Absenteeism</th>
<th>Creativity</th><th>Problem Solving</th><th>Submit</th></tr>";
while($row = mysql_fetch_array($result)){
$ict_id= $row['ictv_id'];
$name= $row['ictv_name'];
$lb= $row['lb_name'];
echo "<tr><form name=\"form$ict_id\" id=\"form$ict_id\"><td>".$name."</td><td>".$lb."</td>
<td>
<select name=\"absent$ict_id\" id=\"absent$ict_id\" style=\"width: 200px\">
<option value></option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>
<td>
<select name=\"creative$ict_id\" id=\"creative$ict_id\" style=\"width: 200px\">
<option value></option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>
<td>
<select name=\"problem$ict_id\" id=\"problem$ict_id\" style=\"width: 200px\">
<option value></option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>
<td>
<input type=\"button\" name=\"submit$ict_id\" id=\"submit$ict_id\" value=\"Submit\" class=\"btn btn-primary \" onclick=\"getVal(this.id)\">
</td>
</form>
</tr>";
}
echo "</table><br /><br /></div>";
// mysql_close();
?>
My query is how can I use Value of selVal1 and selval2 in getVal function without passing argument.
Share Improve this question edited Nov 15, 2014 at 8:14 stackuser asked Nov 15, 2014 at 6:36 stackuserstackuser 933 silver badges13 bronze badges 6- 1 Why not use global variables then? Make selVal1 and selval2 as global. – Sajib Acharya Commented Nov 15, 2014 at 6:40
- If you're using jQuery, show us how these functions are called, that way you can probably avoid globals. – adeneo Commented Nov 15, 2014 at 7:04
- @SajibAcharya I made selVal1 and selVal2 as global variable but it's value aren't displayed in getval function – stackuser Commented Nov 15, 2014 at 8:18
- @stackuser did you try Rushee's solution? – Sajib Acharya Commented Nov 15, 2014 at 9:57
- @SajibAcharya I have tried but it didn't sort out my problem – stackuser Commented Nov 15, 2014 at 14:23
3 Answers
Reset to default 4var selVal1 = "";
var selVal2 = "";
function getvalyear(ctrl1){
var sel = ctrl1.selectedIndex;
selVal1 = ctrl1.options[sel].value;
}
function getvaltrimester(ctrl2){
var sel = ctrl2.selectedIndex;
selVal2 = ctrl2.options[sel].value;
}
function getVal(clicked_id)
{
alert(selVal1 );
alert(selVal2 );
}
your getvalyear()
and getvaltrimester()
both performs the same functionality so,it can be just a single function.
var selected_array = [];//array variable
function getvalyear(ctrl){
var sel = ctrl.selectedIndex;
selected_array.push(ctrl.options[sel].value);
}
function getval(clicked_id)
{
for(var i=0; i<selected_array.length; i++) {
alert(selected_array[i]);
}
}
Hope this you satisfy your needs.Here Working FIDDLE
Check this option:
You need to add those variables to a high level scope. You can make those vars as globals (I don't remend that) or you can wrap your code in a self executable function that is a better practice.
Take a look:
(function(){
var selVal1 = '',
selVal2 = '';
function getvalyear(ctrl1){
var sel = ctrl1.selectedIndex;
selVal1 = ctrl1.options[sel].value;
}
function getvaltrimester(ctrl2){
var sel = ctrl2.selectedIndex;
selVal2 = ctrl2.options[sel].value;
}
function getVal(){
console.log(selVal1);
console.log(selVal2);
}
})();
Here is a codepen with the code updates. (Attaching events using jQuery)