I'm trying to store variables from select/option, and then access them from a button to send to a javascript function that would filter some divs on my page.
So far I have it sending the value to "filter()" when the value changes.
Here's my markup:
<select name="Area" onchange="filter(this)">
<option selected>Select</option>
<option value="Austin">Austin</option>
<option value="San Antonio">San Antonio</option>
<option value="Temple">Temple</option>
</select>
<select name="Number" onchange="filter(this)">
<option selected>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<a class="button">Submit</a>
but how do I store the values from all the options, and then send them
all at once to the function?
Edit: In other words, how do I store the value of each option, and only send to the function once they click submit?
If I got it right, without jQuery you may try this:
document.getElementById("button").onclick = function() {
var selects = document.body.getElementsByTagName("select");
var data = [];
for (var i = 0; i < selects.length; i++) {
data.push(selects[i].value);
}
destinyFunction(data);
};
Or you may use jQuery for the sake of simplicity:
$("#jbutton").on("click", function() {
var data = [];
$("select").each(function() {
data.push($(this).val());
});
destinyFunction(data);
});
Fiddle with those two examples here.
Give each of your selects an id attribute, like:
<select name="Number" onchange="filter(this)" id="Number">
...
Then you can get the value of each in javascript by:
var el = document.getElementById('Number');
var value = el.options[el.selectedIndex].value;
As a sidenote, using onchange in HTML markup to attach your event handling is not best practice. This article from quirksmode offers a good explanation of alternative solutions. However, there are major cross-browser considerations to be taken into account, which is why most people prefer to use a Javascript framework so that those are mostly mitigated.
Related
The option choosen by the user from select box, it have to be send to the desired php file (getStatus.php) may be with get method or post method but without using form submit button
<select name="status">
<option value="step_1">step_1</option>
<option value="step_2">step_2</option>
<option value="step_3">step_3</option>
</select>
Go
You will have a var called $_POST['status'] or $_GET['status'] with the value equal to the value of the selected option. Without more code that's about all I can tell you.
EDIT: This will need to be made in a form with the action attribute set to getStatus.php and preferably a method of POST. There will not be any way around using a form if you want to pass data to another page unless you pass the data with AJAX. Why are forms not an option?
use javascript for this :-
<select name="status" id="myselect">
<option value="step_1">step_1</option>
<option value="step_2">step_2</option>
<option value="step_3">step_3</option>
</select>
Go
<script>
function click_me(){
var selects = document.getElementById("myselect");
var selectedValue = selects.options[selects.selectedIndex].value;// will gives u 2
var selectedText = selects.options[selects.selectedIndex].text;// gives u value2
window.location = "getStatus.php?id="+selectedText;
}
</script>
<select name="status" id="status">
<option value="step_1">step_1</option>
<option value="step_2">step_2</option>
<option value="step_3">step_3</option>
</select>
Go
<script>
function getitem(){
var item = document.getElementById('status').value;
location.href="getStatus.php?id="+item;
}
</script>
My code reads 2 data from the database, and if those data are specific value, then it should remove values 2,3,4,5,6,7,8 from the select called smjer. Values from database are read in external .php script, and then sent back to web page using GET. My problem is next: In one specific case, when the second select in code is set to 3, then code should remove only 2nd and 3rd value from select smjer. I've done this with jQuery,and it works ,but my problem is that when i change value of other select, for example, from 2, I select value 3, nothing happens, so I have to refresh the page, so my changes would apply.
Selects look like this:
<select name="smjer" class="smjer">
<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>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
Second select:
<select name="godina" class="upis">
<option value="1" >1.</option>
<option value="2" >2.</option>
<option value="3">3.</option>
</select>
And the code for removal:
<?php }
if(($_GET['program1'])=='1'&&($_GET['stupanj_spreme1']=='2'))
{?>
<script>
$(document).ready(function(e) {
var ary=[2,3,4,5,6,7,8];
var provjera=$('.upis').val();
if(provjera=='3')
{
var ary=[2,3];
}
$('[name=smjer] option').filter(function(){
return ($.inArray(parseInt(this.value),ary) >-1);
}).remove();
});
</script>
<?php } ?>
I have been reading on net that something like this should be done with AJAX, but I don't know anything about AJAX, an I would like to avoid it. Tnx in advance.
Instead of running your javascript once onready, bind it to the change event of the select in question:
$(document).ready(function(e) {
// When the value of the select changes, run this:
$('.upis').change(function(){
var ary=[2,3,4,5,6,7,8];
var provjera=$('.upis').val();
if(provjera=='3')
{
var ary=[2,3];
}
$('[name=smjer] option').filter(function(){
return ($.inArray(parseInt(this.value),ary) >-1);
}).remove();
});
});
Also, best practice: if you don't want "upis" to EVER apply to another element on this page, consider using id instead of class.
ive run into a quite tricky problem while using a method to set the form action when value changes. I have got the following code :
var actions = new Array();
actions[0] = "some-site.html";
actions[1] = "some-other-site.html";
actions[2] = "another-site.html";
etc....
$("select#feld-urlaubsart").change(function () {
if ($("select#fieldname").val() !== "") {
$("form#searchform").attr("action","http://www.mysite.com/" +
actions[$("select#fieldname").val()]);
}
});
On the other side, my select looks like this :
<select id="fieldname" name="fieldname">
<option value="0" selected="selected">All</option>
<optgroup label="Golfurlaub mit Greenfees">
<option value="1">Alle Greenfee-Angebote</option>
<option value="2">Top-Angebote</option>
<option value="3">Golfurlaub mit 4 Tage Greenfee</option>
</optgroup>
Actually it does work but i am not happy with it as i need to rely on the value="1" value="2" etc.
Now i need to use the actual description as value to be compatible with another script that i pass the value to for usage in PHP/JSON. I would need the data like this
<option value="All">All</option>
<option value="Option 1">Option 1</option>
etc
How can i change the action of the form based on the selected VALUE while not needing to use array keys like "1" "2" etc?
Thanks for any advice.
Use a json object:
actions = {"All":"some-site.html","Option1":"some-other-site.html"}
Then
actions["All"]
is
"some-site.html"
I am trying to be able to change the value of a hidden field from multiple combo boxes.
Here is the example, I want to generate the value of the hidden field name="myNumbers" with the three previous select elements delimited by |.
How can I do this? with jQuery changing the .val() or with .attr() or maybe with a simple variable in PHP?
<select name="element1">
<option value="">Select One</option>
<option value="1">1</option>
<option value="2">2</option>
<option selected value="3">3</option>
<option value="4">4</option>
</select>
<select name="element2">
<option selected value="">Select One</option>
<option value="1">1</option>
<option selected value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="element2">
<option selected value="">Select One</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option selected value="4">4</option>
</select>
<input type="hidden" name="myNumbers" value="3|2|4">
This will do what you want...
$(function(){
var str = $('element1').val() + '|' + $('element2').val() + '|' + $('element3').val()
$("[name='myNumbers']").val(str)
})
EDIT
A more complete solution that will iterate over every select element on the page
$(function(){
$('select').change(function(e){
var myNumbers = [];
$('select').each(function(){
myNumbers.push($(this).val());
})
$("[name='myNumbers']").val(myNumbers.join('|'));
});
});
Here it is http://jsfiddle.net/fuhQx/
You have to have an onChange function for each drop down that calls updateMyNumbers function
function updateMyNumbers() {
$("#myNumbers#").val(("#element1").val() +"|"+("#element2").val()+"|"+("#element3").val());
}
var select = document.getElementById('#element1');
var index = select.selectedIndex;
With this code, you can build the string for the hidden input
If you are wanting to set it once at the time the page is loaded, it would make sense to do it in php (see answers above).
If you want to do it whenever a user changes one of the selects, then you would want to use javascript (jQuery is one good way) to do that in response to an onChange event for any of those selects.
If you just want to do it when the form is submitted, you could also just use javascript (perhaps jQuery in particular) to do it as part of a general form validation for the onsubmit event.
If you want it to happen just prior to using the form values to store something in your database, again you would do it in php once the user has selected things and hit Submit. But then you really don't need the hidden value at all, you can just create it in php after you have the form submitted.
You would have to use javascript as php is run server-side, so before the page is loaded or after it has been submitted.
In text: You would have to capture the change events of the select boxes, generate your string and put that string in the hidden element.
Untested code:
$("select").change(function(){
var desired_value = $("[name='element1']").val() + "|" + $("[name='element2']").val() + "|" + $("[name='element3']").val();
$("[name='myNumbers']").val(desired_value);
});
Hmm...this question may sounds silly , hope you all don't mind...
If I have a drop list:
<select name="myoption" onchange="document.textbox.value=this.value">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<input type="text" name="textbox" id="textbox">
So with this now the textbox will display what is selected , but is it possible to display the A,B,C instead of 1,2,3?
Actually I need a drop-list which will display 2 different values to 2 textbox,such as if A is selected,textbox1 will display "A" and textbox2 will display "1".
I don't know if it is possible and I tried for some times already...can someone give me some hints?
Thanks in advance.
Yes, it is possible. I would just externalize all javascript into a separate file to avoid mixing markup and scripts.
So the script:
// subscribe for the DOM ready event to ensure that you
// are manipulating the DOM only when it is loaded
window.onload = function() {
// subscribe for the onchange event of the dropdown
document.getElementById('myoption').onchange = function() {
// fetch the text of the currently selected element
var text = this.options[this.selectedIndex].innerHTML;
// and assign it to the corresponding input
document.getElementById('textbox').value = text;
};
};
and the markup:
<select name="myoption" id="myoption">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<input type="text" name="textbox" id="textbox" />
and a live demo.