I am using dropdownlist and one label in my html page. I want to change name of label
on selection of my dropdownlist box. How is it possible?
pls povide any help for me .
Check this example.
It will change the attribute 'name' of your label, and its text content (reading its new name attribute value) to see the change in effect.
<script type="text/javascript">
function changeValue(select) {
var value = select.options[select.selectedIndex].value;
var label = document.getElementById('mylabel');
label.setAttribute('name', value);
label.innerHTML = label.getAttribute('name');
}
</script>
<select name="selectcity" onchange="changeValue(this);" >
<option>Mumbai</option>
<option>Delhi</option>
</select>
You selected: <label id="mylabel" name="citylabel"></label>
<select id='derp' onchange="changeVal(this,'changeme');"> <!-- declare select, set onchange to point to changeVal JS -->
<option value='test'>test</option>
</select>
<input id='changeme' />
<script type='text/javascript'>
function changeVal(el,changeElID){
var changeEl = document.getElementById(changeElID); // Get input to change
changeEl.value = el.options[el.selectedIndex].value; // Change input value to value of selected index
}
</script>
[EDIT]
re-reading the question it sounds like you are trying to change the name of the input box...if that is the case, change changeEl.value to changeEl.name
HTML:
<table cellspacing="0" cellpadding="1" rules="all" border="0" id="gvLanguage">
<tr>
<td align="left">
<select id="ddlLanguage" style="width: 230px;">
<option value="0">[Select]</option>
<option value="1">Afrikaans</option>
<option value="2">Akan</option>
<option value="3">Albanian</option>
<option value="4">American</option>
</select>
</td>
</tr>
<tr>
<td>
Selected Value: <label id="lblChange" ></label>
</td>
</tr>
</table>
JQUERY:
$(document).ready(function(){
$("#ddlLanguage").change(function(){
var vSelectedValue = $("option:selected",$(this)).text();
$("#lblChange").text(vSelectedValue);
});
});
CLICK HERE TO SEE THE DEMO
Related
I have a drop down box, this is populated with options that after selecting it shows hidden text by calling the function toggletDisplay() and sending the value of the option throug, I want it to be able to do the same but without the drop down box to select, using instead plain text with onclick() instead of onchange() or something similiar.
Current Code
<form id="criteria" name="criteria">
<table width="200px" height="700px" name="criteria_search" align="left" border="1" style="margin-right:70px">
<tr>
<td class="dataLabel" width="100%" align="left"><strong>Add Rule : </strong>
<select name="rule" id="rule" onChange="toggletdDisplay(this.form);">
<optgroup label="Simple Rules">
<option value="instructions" selected="selected"> </option>
<option value="email">Email</option>
<option value="assigned">Assigned Racecourse</option>
</optgroup>
</select>
</td>
</tr>
</table>
<table align="right" border="1" width="300px" height="400px" style="float:left;">
<tr>
<td class="dataLabel" name="assigned" id="assigned" style="display: none;">
<table border="0">
<tr>
<td colspan="3"><h4>Assigned to Racecourse</h4></td>
</tr>
<tr>
<td style="margin-left:20px">
<b>Assigned To: </b><select name="selected_assigned_location" id="selected_assigned_location"></select>
</td>
</tr>
</table>
</td>
<td width="100px" class="dataLabel" name="email" id="email" style="display: none;" >
<table>
<tr>
<td colspan="3"><h4>Registered Email</h4></td>
</tr>
<tr>
<td><b>Do they have a registered Email Account?</b></td>
</tr>
<tr>
<td>
Yes <input type="radio" name="email_c" value="true_ex" {EMAIL_TEX_CHECKED} checked="checked"> No <input type="radio" name="email_c" value="false" {EMAIL_F_CHECKED}>
</td>
</tr>
</table>
</td>
...ect
I tried just sending the value through as an onclick
<td>
<p id="rule" name="rule" value="email" onclick="toggletdDisplay(this.form);">Email</p>
</td>
But I get an error of value rule is undefined. How would I send the value through the same as before but without using a select statement?
Added the toggletDisplay, simply uses the value sent back to change the style of the datalabel from hidden to inline
function toggletdDisplay(me)
{
list = Array("instructions","sex", "email", "mobile", "account", "age", "location", "spent", "booked_anything", "internet_booked", "package_type", "package_name", "booked_location", "new_booked_event", "booked_event_range","team", "no_reorder", "newsletter","hear_about","hear_about_bookings","mosaic_group","mosaic_type","assigned","assigned_user","lead_source","target_list","awc","birthday");
// hide any previously selected elements
for(x=0; x<list.length; x++)
{
deselect = getElementsByName_iefix("TD", list[x]);
for (j=0; j<deselect.length; j++)
{
deselect[j].style.display = "none";
}
}
// display currently selected criteria
selected = getElementsByName_iefix("TD", me.rule.value);
selected[0].style.display = "inline";
}
There seem to be a number of issues with your code. One of them is the undefined function toggletdDisplay() that is called whenever you change the selection in your select field.
But, basically, if you want to send a value of an input field or a select box within a form to a php script on your server you will need to define an action attribute in your <form> tag and make sure that the form is submitted. This can be achieved in your case by changing the onchange attribute in your select box (simplified code, without the table architecture):
Whenever you change the selection in your select box the form will be submitted and the value of that select box will be sent to target.php. The address line in your browser will show something like
...<your URL>/target.php?rule=email
It is also not clear to me why you use colspan attributes in some of your <td>-elements, as there is only one column to display in that table.
My advice is to be economical with "cut and paste" and only use code that you fully understand. Build your page slowly, step by step. That way you will be able to understand what needs to be fixed if something goes wrong.
Edit
With your toggletdDisplay() script we have something to work on. The first thing that springs to my mind is that you are not using jquery functions where they might be helpful. And secondly, you don't do anything to display the form values in the console window or send them to a php script.
It is also important to note that name attributes can only be assigned to <input> or <select> elements and not to <td> elements. In my following script I used the id attribute instead.
var tds,tdis;
$(function(){
var list = ["instructions","sex", "email", "mobile", "account", "age", "location", "spent", "booked_anything", "internet_booked", "package_type", "package_name", "booked_location", "new_booked_event", "booked_event_range","team", "no_reorder", "newsletter","hear_about","hear_about_bookings","mosaic_group","mosaic_type","assigned","assigned_user","lead_source","target_list","awc","birthday"];
// consider only TDs with IDs from list array:
tds= $('td').filter(function(i,el){return $.inArray(el.id,list)>-1;});
// trigger the display of results only for select and input elements within tds:
tdis=$('select,input', tds).on('change',listResults);
// assign the toggletdDispla function to the rule selector:
$('.action').on('change',toggletdDisplay);
});
function toggletdDisplay(){ tds.hide().filter('#'+this.value).show()}
function listResults(){
$('#show').html('<p>Values to be sent:</p>'+tdis.serialize().replace(/&/g,'<br/>'))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="target.php">
<table name="criteria_search" align="left" border="1" style="margin-right:70px">
<tr><td class="dataLabel"><strong>Add Rule : </strong>
<select name="rule" id="rule" class="action">
<optgroup label="Simple Rules">
<option value="instructions" selected="selected"> </option>
<option value="email">Email</option>
<option value="assigned">Assigned Racecourse</option>
</optgroup>
</select>
</td><td class="dataLabel" id="email" style="display:none">
<b>email:</b>
<br/><label><input type="radio" name="email_c" value="true_ex"> yes</label>
<br/><label><input type="radio" name="email_c" value="false"> no</label>
<br/><label><input type="radio" name="email_c" value="soon"> not yet</label>
</td>
<td class="dataLabel" id="assigned" style="display:none">
Racecourse<br/>
<b>Assigned To: </b>
<select name="selected_assigned_location">
<option value=""></option>
<option value="a">racecourse A</option>
<option value="b">racecourse B</option>
<option value="c">racecourse C</option>
</select>
</td>
</tr>
</table>
</form><br style="clear:both" />
<div id="show"></div>
I would like to make a list of select tag that they have two values, yes and no.
I've created it(with a while statement in the php).
now, I want to create a select tag with two values(yes and no) that it should change all select tags.ex: if I select yes option, all options must be yes.
<input onclick="toggle(this)" id="#select-all" type="checkbox" name="one"/><p>all</p>
<select data='beauty' name="txt" id="txt" onchange="setSelectBoxByText(this.getAttribute('data'), this.options[this.selectedIndex].text);">
<option value="id_yes">yes</option>
<option value="id_no">no</option>
</select>
<br />
<br />
<br />
<table style="width:100%">
<?php
$result = mysql_query("my query...");
while($row=mysql_fetch_object($result)){
echo "<tr>
<td>
<input type='checkbox' name='one'/><p style='display:inline;font-size:14px;color:#fff'><a style='color:#fff' href='#'>$row->title</a></p>
</td>
<td align='left' style='padding-left:2px'>
<select name='beauty' id='beauty'>
<option value='id_yes'>yes</option>
<option value='id_no'>no</option>
</select>
</td>
</tr>
";
}
?>
</table>
You can set class on selects created in while and on select tag you want attach onchange event which will get all elements by className and set selection index same as your select tag selected index.
<select id="globalSelection" onchange="changeSelection(this)">
<option value="id_yes">yes</option>
<option value="id_no">no</option>
</select>
var selects = document.getElementsByClassName('mySelect');
function changeSelection(globalSelect) {
for (var i = 0, select; select = selects[i]; i++) {
select.selectedIndex = globalSelect.selectedIndex;
}
}
I have html with 2
<select> ... </select>
elements inside a span with class spanClass. Now i am trying to select those selects with jQuery with this code:
jQuery(document).ready(function() {
spans = jQuery('.spanClass');
spans.each(function() {
var inputs = jQuery(this).find('select');
console.log(inputs);// This is working
inputs.each(function() {
alert('test'); //This not
});
});
});
HTML:
<table>
<tr>
<td>
<select name="een">
<option> test </option>
</select>
</td>
</tr>
<select name="twee">
<option> test </option>
</select>
</td>
</tr>
</table>
However, this is not working, can anybody tell me why?
First> Put Table inside Div instead of Span (it's the correct way to do this)
Second> Correct your table tags as the following image and codes (some of them are incorrect!)
Now> Use these codes
HTML:
<div class="divClass">
<table>
<tr>
<td>
<select name="een">
<option> test </option>
</select>
</td>
</tr>
<tr>
<td>
<select name="twee">
<option> test </option>
</select>
</td>
</tr>
</table>
</div>
jQuery:
jQuery(document).ready(function() {
spans = jQuery('.divClass');
spans.each(function() {
var inputs = jQuery(this).find('select');
console.log(inputs);
inputs.each(function() {
console.log(jQuery(this).prop("name"));
});
});
});
Result:
var inputs = jQuery(this).find('select'); // Isn't inputs empty jQueryObject? length 0?
if inputs is empty jQueryObject, the callback function passed by .each() is never called.
I am trying to copy a name of the selected html form option field to another field using jquery.
This is what i got now:
script
$(document).ready(
function()
{
//check for change on the categories menu
$('#categories').change(function() {
//get category value
name = $('#categories').text();
$('#name').val(name)
});
});
HTML
<form action="editcat.pnp" method="post" accept-charset="utf-8">
<table>
<tr>
<td><label for="category">Category:</label></td><td><select name="category_id" id="categories">
<option value="1">Info</option>
<option value="2">Resr</option>
<option value="3">Pro</option>
<option value="4">Geo</option>
<option value="5">Site's</option>
<option value="6">Well</option>
<option value="7">Link</option>
<option value="#" selected="selected">Please select</option>
</select></td>
</tr>
<tr>
<tr>
<td><label for="name">Name:</label></td><td><input name="name" type="text" size="40" maxlength="40" id="name"></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Update"> <input type="reset"></td>
</tr>
</table>
</form>
The problem is that the name field now contains all the names from the option dropdown list and not only the one i selected.
You should use :selected Selector to get selected option, and then .text() to get its name.
var name = $('#categories option:selected').text();
try this....
$(document).ready(
function()
{
//check for change on the categories menu
$('#categories').change(function() {
//get category value
name = $("#categories option:selected").text();
$('#name').val(name)
});
});
Hope its help
I think you are looking to use $("#categories").val() instead of .text().
Example: http://jsfiddle.net/shanabus/wZAM7/
Read jQuery Docs and shown Examples in the .change() function.
I am a novice at JavaScript and jQuery. I want to show one combobox-A, which is an HTML <select> with its selected id and contents at the other place on onChange().
How can I pass the complete combobox with its select id, and how can I pass other parameters on fire of the onChange event?
function getComboA(selectObject) {
var value = selectObject.value;
console.log(value);
}
<select id="comboA" onchange="getComboA(this)">
<option value="">Select combo</option>
<option value="Value1">Text1</option>
<option value="Value2">Text2</option>
<option value="Value3">Text3</option>
</select>
The above example gets you the selected value of combo box on OnChange event.
Another approach wich can be handy in some situations, is passing the value of the selected <option /> directly to the function like this:
function myFunction(chosen) {
console.log(chosen);
}
<select onChange="myFunction(this.options[this.selectedIndex].value)">
<option value="1">Text 1</option>
<option value="2">Text 2</option>
</select>
For how to do it in jQuery:
<select id="yourid">
<option value="Value 1">Text 1</option>
<option value="Value 2">Text 2</option>
</select>
<script src="jquery.js"></script>
<script>
$('#yourid').change(function() {
alert('The option with value ' + $(this).val() + ' and text ' + $(this).text() + ' was selected.');
});
</script>
You should also know that Javascript and jQuery are not identical. jQuery is valid JavaScript code, but not all JavaScript is jQuery. You should look up the differences and make sure you are using the appropriate one.
JavaScript Solution
<select id="comboA">
<option value="">Select combo</option>
<option value="Value1">Text1</option>
<option value="Value2">Text2</option>
<option value="Value3">Text3</option>
</select>
<script>
document.getElementById("comboA").onchange = function(){
var value = document.getElementById("comboA").value;
};
</script>
or
<script>
document.getElementById("comboA").onchange = function(evt){
var value = evt.target.value;
};
</script>
or
<script>
document.getElementById("comboA").onchange = handleChange;
function handleChange(evt){
var value = evt.target.value;
};
</script>
I found #Piyush's answer helpful, and just to add to it, if you programatically create a select, then there is an important way to get this behavior that may not be obvious. Let's say you have a function and you create a new select:
var changeitem = function (sel) {
console.log(sel.selectedIndex);
}
var newSelect = document.createElement('select');
newSelect.id = 'newselect';
The normal behavior may be to say
newSelect.onchange = changeitem;
But this does not really allow you to specify that argument passed in, so instead you may do this:
newSelect.setAttribute('onchange', 'changeitem(this)');
And you are able to set the parameter. If you do it the first way, then the argument you'll get to your onchange function will be browser dependent. The second way seems to work cross-browser just fine.
jQuery solution
How do I get the text value of a selected option
Select elements typically have two values that you want to access.
First there's the value to be sent to the server, which is easy:
$( "#myselect" ).val();
// => 1
The second is the text value of the select.
For example, using the following select box:
<select id="myselect">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>
If you wanted to get the string "Mr" if the first option was selected (instead of just "1") you would do that in the following way:
$( "#myselect option:selected" ).text();
// => "Mr"
See also
.val() jQuery API Documentation
This is helped for me.
For select:
$('select_tags').on('change', function() {
alert( $(this).find(":selected").val() );
});
For radio/checkbox:
$('radio_tags').on('change', function() {
alert( $(this).find(":checked").val() );
});
You can try bellow code
<select onchange="myfunction($(this).val())" id="myId">
</select>
Html template:
<select class="staff-select">
<option value="">All</option>
<option value="196">Ivan</option>
<option value="195">Jon</option>
</select>
Js code:
const $staffSelect = document.querySelector('.staff-select')
$staffSelect.onchange = function () {
console.log(this.value)
}
Just in case someone is looking for a React solution without having to download addition dependancies you could write:
<select onChange={this.changed(this)}>
<option value="Apple">Apple</option>
<option value="Android">Android</option>
</select>
changed(){
return e => {
console.log(e.target.value)
}
}
Make sure to bind the changed() function in the constructor like:
this.changed = this.changed.bind(this);
this code once i write for just explain onChange event of select you can save this code as html and see output it works.and easy to understand for you.
<html>
<head>
<title>Register</title>
</head>
<body>
<script>
function show(){
var option = document.getElementById("category").value;
if(option == "Student")
{
document.getElementById("enroll1").style.display="block";
}
if(option == "Parents")
{
document.getElementById("enroll1").style.display="none";
}
if(option == "Guardians")
{
document.getElementById("enroll1").style.display="none";
}
}
</script>
<form action="#" method="post">
<table>
<tr>
<td><label>Name </label></td>
<td><input type="text" id="name" size=20 maxlength=20 value=""></td>
</tr>
<tr style="display:block;" id="enroll1">
<td><label>Enrollment No. </label></td>
<td><input type="number" id="enroll" style="display:block;" size=20 maxlength=12 value=""></td>
</tr>
<tr>
<td><label>Email </label></td>
<td><input type="email" id="emailadd" size=20 maxlength=25 value=""></td>
</tr>
<tr>
<td><label>Mobile No. </label></td>
<td><input type="number" id="mobile" size=20 maxlength=10 value=""></td>
</tr>
<tr>
<td><label>Address</label></td>
<td><textarea rows="2" cols="20"></textarea></td>
</tr>
<tr >
<td><label>Category</label></td>
<td><select id="category" onchange="show()"> <!--onchange show methos is call-->
<option value="Student">Student</option>
<option value="Parents">Parents</option>
<option value="Guardians">Guardians</option>
</select>
</td>
</tr>
</table><br/>
<input type="submit" value="Sign Up">
</form>
</body>
</html>
function setMyValue(v) {
console.log(v);
}
<select onchange="setMyValue(this.value)">
<option value="a">1</option>
<option value="b">2</option>
<option value="c">3</option>
</select>
This worked for me onchange = setLocation($(this).val())
Here.
#Html.DropDownList("Demo",
new SelectList(ViewBag.locs, "Value", "Text"),
new { Class = "ddlStyle", onchange = "setLocation($(this).val())" });
Simply:
function retrieve(){
alert(document.getElementById('SMS_recipient').options[document.getElementById('SMS_recipient').selectedIndex].text);
}
function retrieve_other() {
alert(myForm.SMS_recipient.options[document.getElementById('SMS_recipient').selectedIndex].text);
}
function retrieve() { alert(document.getElementById('SMS_recipient').options[document.getElementById('SMS_recipient').selectedIndex].text);
}
<HTML>
<BODY>
<p>RETRIEVING TEXT IN OPTION OF SELECT </p>
<form name="myForm" action="">
<P>Select:
<select id="SMS_recipient">
<options value='+15121234567'>Andrew</option>
<options value='+15121234568'>Klaus</option>
</select>
</p>
<p>
<!-- Note: Despite the script engine complaining about it the code works!-->
<input type="button" onclick="retrieve()" value="Try it" />
<input type="button" onclick="retrieve_other()" value="Try Form" />
</p>
</form>
</HTML>
</BODY>
Output:
Klaus or Andrew depending on what the selectedIndex is. If you are after the value just replace .text with value. However if it is just the value you are after (not the text in the option) then use document.getElementById('SMS_recipient').value
//html code
<select onchange="get(this)">
<option value="a">1</option>
<option value="b">2</option>
<option value="c">3</option>
</select>
//javscript code
function get(select) {
let value = select.value;
console.log(value);
}