HTML - MOOTOOLS- Add option to select box in ie6 - mootools

<select id="selectId">
<option>Please select product ...</option>
</select>
i try on firefox and it work
$('selectId').innertHTML = '<option>Test test</option>'
but on ie , it not work, how to add a option by string option like above in ie

Use the Element Class instead:
new Element('option', {
text: 'test option'
}).inject($('selectId'));​
Example: http://www.jsfiddle.net/EJH5b/

If you are going to use Mootools, then you should really use the Mootools methods rather than switch between it and vanilla Javascript. One benefit from doing this is that Mootools is already taking care of browser inconsistencies for you. Therefore, if you ignore its methods, you will have to take care of them yourself.
To access the properties of an elements the Mootools way, you can use the set and get methods on any element. The dollar ($) function returns an element so you can just chain set and get to that.
//returns selectId's HTML
var foo = $('selectId').get('html');
//Sets the HTML of selectId to <option>Test test</option>
$('selectId').set('html', '<option>Test test</option>'); //
In this case, you just need to use set.
One thing you should be aware of it is this does not add an option to a select box but instead replaces everything inside of the select box with an option. Consequently, if you wanted to use this to add multiple options to a select box, it will not work as you are resetting the HTML each time. It does not make much sense to have a select box that only has a single option so I will assume you are trying to append an option, rather than replace everything with an option. In that case, do the following:
//get the current options selectId's options
var options = $('selectId').get('html');
//Add your own option onto the end
options = options + '<option>Test test</option>';
//Set the HTML of selectId to whatever is stored in options
$('selectId').set('html', options);

Related

based on URL parameter, set dropdown option with jQuery

I have found many similar questions on this subject but none really answer my scenario so I decided to post a new question:
I have one page with several hidden sections (the same content in different languages). The hidden section displays based on a dropdown menu of languages (I'm using jQuery for that and it only hides/displays sections on the same page - the page URL stays the same) this is all working. However, I need to find a way to get people to arrive to the page with their language selected, not the default language which is English.
I would use either one of these: (example.com/page/#german) or (example.com/page/?german) but it then needs to hook into the existing jQuery and HTML so that that URL information selects the correct language. How can I do it?
This is the HTML of the language selector:
<select id="language_selector">
<option value="german">Deutsch</option>
<option value="english" selected >English</option>
<option value="spanish">Español</option>
<option value="french">Français</option>
</select>
and this is the jQuery code I am using to display only the selected language HTML content (based on ID):
hideAllDivs = function () {
$("#English").hide();
$("#French").hide();
$("#German").hide();
$("#Spanish").hide();
};
handleNewSelection = function () {
hideAllDivs();
switch ($(this).val()) {
case 'english':
$("#English").show();
break;
case 'french':
$("#French").show();
break;
case 'german':
$("#German").show();
break;
case 'spanish':
$("#Spanish").show();
}
};
I was thinking that the way to go about it would be to get the URL value, select the parameter only, and then pass that to the jQuery to select the right option but I have no clue how to do it, also without breaking my existing language selector (I still want people to be able to change their language manually)
// url : 'example.com/page?language=german';
let language = (new URLSearchParams(window.location.search)).get('language') ?? 'english';
$('#language_selector').val(language);
handleNewSelection();

What HTML element to use for holding hidden values

In my application I have a need to keep some values hidden, for them to be later picked up by jQuery.
I have a following div for keeping map's data (after img is clicked I would like to show a map with 2 markers):
<div class="map-data">
<img src="../map.png">
</div>
In order to do that I need to store 4 hidden values (1_lat, 1_long, 2_lat, 2_long), like:
<label type="hidden" value="56.056180">
What would the right hidden element be for storing such values? Label, p or something else?
Since it seems that you will only use those values client-side, i.e. you are not going to send them back to the server, it seems wrong to me to use an input element.
The comments already have two very good advice, here expanded.
Personally I would use data attributes if I needed per-element data (data that varies per element).
<div id="sampleMap" data-lat1="0.241" data-lat2="0.56">
...
</div>
<div id="anotherSampleMap" data-lat1="0.87" data-lat2="0.283">
...
</div>
They can be accessed very easily.
function configureTheMap(map)
{
var lat1 = map.dataset.lat1;
var lat2 = map.dataset.lat2;
...
}
...
configureTheMap(document.getElementById('sampleMap'));
configureTheMap(document.getElementById('anotherSampleMap'));
You should check browser compatibility though.
If the data was global I would simply generate a JavaScript object
var coordinates_config = {lat1 : 0.241, lat2 : 0.56};
and use it accordingly
function configureTheMap(map)
{
var lat1 = coordinates_config.lat1;
var lat2 = coordinates_config.lat2;
...
}
You can generalize this method easily to be used for per-element data if data attributes are not supported by the browsers you are targeting.
In both cases, allow me to remember the importance of proper escaping when generating the markup/code.
You should probably use an input box of type hidden for this purpose:
<input id='1_lat' type="hidden" value="56.056180">
You can then update this value with $("#1_lat").val("22.0")
<input id='input_ID' type="hidden" value="whateveryouwant">
in dreamweaver you can use it as gui drag and drop from the php toolbox on the right
You can use the input type="hidden".
reference: w3schools
I suggest you always use INPUT for the hidden values.
There are a few alternatives as follow :
<input>element
HTML5 Custom Data Attributes
Custom non-visible data with data attributes

Are there any techniques to indicate if a html select element is "used"?

Imagine I have some webapp with a lot of different filters, which impact e.g. search results on a page.
The filters are optional and I use select element. The first option always means, that the filter is not used and says something like "not used" or "select xyz".
I'm looking now for a way to make it possible to see in a quick glance which filters are currently used and which are not, e.g. by changing the color or background-color.
I didn't find a way to do this via CSS yet, maybe its not possible without javascript?
Solutions needs only to work in latest Chrome.
If you want the color of the dropdown to change immediately as soon as the user changes the selection (without reloading the page), you'll have to use JavaScript. Something like:
<script type="text/Javascript">
function changeColor(element)
{
if (element.selectedIndex != 0)
element.style.backgroundColor = "green";
else
element.style.backgroundColor = "red";
}
</script>
<select onchange="changeColor(this)">
So that's how you would change the color as soon as the user changes the selection.
To change it on page reload, you would have to use some server-side scripting language (which I'm assuming you're using already). So when creating the <select> element, you would check to see if its value has been set. If you were using PHP, it might look something like this:
<select name="mySelect" style="background-color:<?php echo ($_POST["mySelect"] == "select xyz" ? "red" : "green"); ?>;">

strange settings/options page of Chrome extension

I am reading a Chrome extension codes. In one of its html files (used for setting), the source code shows that there is a select element like this
<select id="hostselect"></select>
Note that there is no option element as the child of the select element. However, if I open the setting page of the extension in Chrome, there is a drop-down list corresponding to that select element, and moreover, if I use the Chrome developer tool to inspect this setting page, it shows that the select element has option children:
<select id="hostselect">
<option value="host1">host1</option>
<option value="hots2">host2</option>
</select>
I don't understand why there is no option child if I view the source code of the setting page in text editor, while the option children appear when I view the page's DOM using Chrome developer tool. What could be the most possible reason for this? Could some content scripts in the extension change the DOM of the setting page? Also, what's the benefit to write codes like this?
Thanks!
I do this for an extension at work. I am using Javascript to load extra options in to the select. I do this because the number of options is dynamic. I don't have source available now but I can post an example later if you would like
Update
Here is an example of some javascript that might populate your select element. I am using jQuery to append the options and to iterate over my list of hosts with jQuery's each. I have two static options "Default" and "None" the the dynamic list which was generated with getHosts()
var hosts = getHosts();
if(hosts) {
var selectElement = $("#hostSelect");
selectElement.append($("<option></option>").attr("value", "0").text("-- Default Host --"));
selectElement.append($("<option></option>").attr("value", "-1").text("-- No Host Selected --"));
$.each(hosts, function(key, host) {
selectElement.append($("<option></option>").attr( "value", host.getHostID() ).text( host.getName() ));
})
}

Lock HTML select element, allow value to be sent on submit

I have a select box (for a customer field) on a complex order form, when the user starts to add lines to the order they should not be allowed to change the customer select box (unless all lines are deleted).
My immediate thought was that I could use the disabled attribute, but when the box is disabled the selected value is no longer passed to the target.
When the problem arose a while ago one of the other developers worked around this by looping through all the options and disabling all but the selected option, and sure enough the value was passed to the target and we've been using since. But now I'm looking for a proper solution, I don't want to loop through all the options because are data is expanding and it's starting to introduce performance issues.
I'd prefer not to enable this / all the elements when the submit button is hit.
How can I lock the input, whilst maintaining the selected option and passing that value to the target script? I would prefer a non-JavaScript solution if possible, but if needed we are running jQuery 1.4.2 so that could be used.
Edit
I've tried to use the readonly attribute with little success, here's the jQuery code I'm using:
$('.abc').each(function(element) {
$(this).attr('readonly','readonly');
});
When inspecting the element with Firebug the readonly attribute had been set, but I can still change the value in the select box?!
This works:
$('.abc :not(:selected)').attr('disabled','disabled');
jQuery will still be looping through the elements behind the scenes, but I seriously doubt you will have performance issues unless your select has thousands of elements (in which case your usability issues will far out weigh the performance issues). The original code must have been doing something wrong.
This works fine
<select disabled="true">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">thre</option>
</select>
Add a hidden field to your form and onsubmit take the value from the select and place it in the hidden field's value
As per the HTML spec, readonly is not supported by the select tag
The element does not accept readonly attribute. Readonly is a wrapper that fix this.
Try this:
https://github.com/haggen/readonly
"select" does not have a readonly attribute. It has only disabled attribute.
http://www.w3schools.com/TAGS/att_select_disabled.asp
So your best best is:
$('.abc').each(function(element) {
$(this).attr('disabled','disabled');
});
HTH
I'd have an idea, that was functional to me:
In my case, when a user selects an option (an account) in a drop-down on a form of an accounting system, e.g., some kind of "expense", that I know that may not be "credited", just "debited", another drop-down that selects the accounting operation (Debit/Credit), changes these drop-down to "Debit".
Then, I "lock" (using "disabled=true") these last "drop-down" in the "debit" option.
The problem that occurred to me these moment, was similar of yours: after disabling the drop-down element, I couldn't receive it in the target, anymore.
So, what I've done:
1 - Changed the option in the second drop-down list, as I said:
document.getElementById("operation").value = "D";
`
2 - Disabled that dropdown:
document.getElementById("operation").disabled = true;
Then, the "cat salt":
3a- Added to the "FORM" element, an "onsubmit"
onsubmit = "validForm()"
3b - On my [java-script] file I added the ["valid-Form"] function:
function validForm()
{
document.getElementById("operation").disabled = false;
}
Voilá!
A simple way to disable any Select is to just disable mouse interaction.
For example:
<select id="complaint_status" name="complaint_status" class="disabledSelect" value="Pending">
<option value="Pending" selected>Pending</option>
<option value="Complete">Complete</option></select>
css
.disabledbutton {
pointer-events: none;
opacity: 0.4;}
The value of Select will be SUBMITted.
Hope it works!!