What I am interested in getting is $option_value['price'] in a string from the dropdown, not . Using .value returns the later.
I have select menus that are created in a foreach() loop. The drop downs will hold price options. I am trying to give each product it's own id so I can use the javascript onChange() function to update prices on the screen.
The select tag looks like this:
<select name="option[<?php echo $option['product_option_id']; ?>]" class="select_options" id="select_<?php echo $product['product_id']; ?>" onchange="getIt(this);">
and the javascript:
function getIt(el) {
var id = el.id;
a = document.getElementById(id).text;
alert(a);
}
Alerting out id gives me select_some number. So I can see that it is getting the id. but alerting out a gives me [object HTMLSelectElement].
so I tried
alert(a.options.selectedIndex.value);
and get unidentified.
<?php if ($product['options']) { ?>
<div class="options" id="option_<?php echo $product['product_id']; ?>">
<?php foreach ($product['options'] as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option select_option">
<select name="option[<?php echo $option['product_option_id']; ?>]" class="select_options" id="select_<?php echo $product['product_id']; ?>" onchange="getIt(this);">
<option value=""><?php echo $text_select; ?></option>
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><span id="newPrice"><?php echo str_replace('.00','.',$option_value['price']); ?></span>)
<?php } ?>
</option>
<?php } ?>
</select>
</div>
<?php } ?>
Change :
a = document.getElementById(id).text;
To:
a = document.getElementById(id).value;
That will alert the value of selected option.
"is there a way I can get the text shown on the drop down ($xx.xx), instead of the value?"
I think this is what you are looking for:
function getIt(el) {
var a = el.options[el.selectedIndex].text;
alert(a);
}
el.selectedIndex gives you, obviously, the index of the currently selected option, which you can use as an index into the collection of options, el.options, and then get that option's text. The text property belongs to each option, not to the select.
Demo: http://jsfiddle.net/FCnUQ/
(Note that if no option is selected selectedIndex will be -1.)
You don't need to use getElementById() at all because you already have a reference to the element, el.
Try this code, put document.getElementById(id).value instead of document.getElementById(id).text
function getIt(id) {
a = document.getElementById(id).value;
alert(a);
}
as you passing this like getit(this) you should code like this
function getIt(el) {
var id = el.id;
a = document.getElementById(id).value;
alert(el.value); //you can directly alert like this
}
Related
I have one dropdown with a foreach loop which will pass to model using post method.
<div class="form-group" ">
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php
foreach ($relationship as $row) {
?>
<option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
<?php
}
?>
</select>
</div>
and in the model it is getting the proper values using post method. As
$rel = $_POST['rel'];
Here the problem is when the user select one option ,I want to get two values like
"<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"
like
$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID
I want to get both from one selection without adding any visble dropdown or element..
Try bellow code with ajax call
In Javascript code get value
var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php foreach ($relationship as $row) { ?>
<option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
<?php } ?>
</select>
</div>
I couldn't keep the selected value after loading the page, that's why I couldn't take the selected value from the select option in Database after done some function..
Here is my code
<select name='courseID' class="mySelect" id='courseID'>
<?php while ($row1 =mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1['CourseID'];?>"><?php echo $row1['CourseID'];?></option><?php endwhile; ?></select>
<script type="text/javascript">document.getElementById('courseID').value="<?php echo $_GET['courseID'];?>";</script>
Did JS can add in a HTML in this way??
You have add condition to check if the course id is in the result.
<?php while ($row1 =mysqli_fetch_array($result1)):;
$selected = ($_GET['courseID'] == $row1['CourseID']) ? 'selected' : '';
?>
<option value="<?php echo $row1['CourseID'];?>" <?php echo $selected;?>><?php echo $row1['CourseID'];?></option>
<?php endwhile; ?>
I am working on a dropdown change event, but I am not able to set a condition for the change event. For the second dropdown(subsel) I want a list of values for which the value in sel is NOT present. So far all I'm able to do is reflect the value in first select dropdown into the second dropdown.
<script>$(document).ready(function() {
$("#sel").live('change', function() {
$("#selsub").val($(this).val());
});
});</script>
<form method="get">
<label for="category">Parent Category</label>
<select name="sel" id="sel">
<?php
include('connection.php');
$query_parent=$query=mysql_query("SELECT * from stations");
<?php while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['name']; ?>"><?php echo $row['name']; ?></option>
<?php endwhile;?>
</select>
<br/><br/>
<label>Sub Category</label>
<div class='detail'>
<select name="selsub" id="selsub">
<?php
$query=mysql_query("SELECT * from stations");
while($row1 = mysql_fetch_array($query)): ?>
<option value="<?php echo $row1['name']; ?>"><?php echo $row1['name']; ?></option>
<?php endwhile; ?>
</select>
</div>
Kindly help me to set a condition where in the second dropdown(selsub) all the values would appear except the value one in first dropdown(sel).
Try to do:
$(document).ready(function() {
$("#sel").on('change', function() {
selectedVal = this.value;
$("#selsub option[value='" + selectedVal +"']").hide();
});
});
I was wondering if someone could explain how to add options to my search results (category, manufacture) etc. I just need them in a select tag (no actions required). I tried similar code to the product options but that didn't work.
Thanks
Found solution: http://www.opencartaz.com/opencart/attribute-options-on-category-page.html
<?php foreach ($products[$j]['options'] as $option) { ?>
<select name="option[<?php echo $option['option_id']; ?>]">
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['option_value_id']; ?>"><?php echo $option_value['name']; ?></option>
<?php } ?>
</select>
<?php } ?>
I Have a select list, each option contains a category ($k[3]) in its id.
<select name="page_from_link" class="my_select_list" >
<OPTION VALUE="" >With:</OPTION>
<?php foreach($pages_created as $k) {
$i=0; ?>
<OPTION id="<?php $i."-".$k[3]; ?>" class="pages_created" VALUE="<?php echo $k[0]; ?>" ><?php echo $k[1]; ?></OPTION>
<?php } ?>
</select>
I have another select list with all options hidden depending on the precedent select category :
AS:
$v) { ?>
" >
my jquery script doesn't work, how would you have done that ?
$(document).ready(function(){
$(".my_select_list").change(function(){
var array = $(this).find('option:selected').attr('id').split('-');
var cat = array[1];
alert("cat");
$("#cat_"+cat).show();
$(".pages_created_option:not(#cat_"+cat).hide();
return false;
});
});
$(this > option).attr(... => $(this).find('option:selected').attr (...
alert("cat"); => alert(cat);
$("#cat_"+cat+")").show(); => $("#cat_"+cat).show();
you should consider using firebug or chrome internal development tools (on F12).
on a side note, you might like to use optgroup instead of a dummy option:
<select name="page_from_link" class="my_select_list" >
<OPTGROUP label="With:">
<?php foreach($pages_created as $k) {
$i=0; ?>
<OPTION id="<?php $i."-".$k[3]; ?>" class="pages_created" VALUE="<?php echo $k[0]; ?>" ><?php echo $k[1]; ?></OPTION>
<?php } ?>
</OPTGROUP>
</select>
you could use either a php script, or a js script with the data encoded, holding the innerHTML and that changes the innerHTML of the parent object (such as a div) depending on what you selected in the previous select box.
add this in the head for javascript
function showsel()
{if(document.getElementById('parentid').value=="id of the option that will triger the shild ")
{document.getElementById('shild id').style.display="inline";}
else {document.getElementById('shild id').style.display="none";}
}
Then add for the shild
div id="shild id" style="display:none"
and to the parent
div id="parent id" onchange"showsel()"