Multiple click listeners - java script - javascript

I am working on a project that imports some javascript rules from a file myjs.js, which is called (on all the web page of the project) in the header.
This files manages the behavior of checkboxes, and in fact toggling the checks of every checkbox pairs. The problem is that in some case, this behavior is wrong but I can't change anything in this js file because it is too complex.
So, on some page, I decided to listen to the click event on some checkbox to correct the behavior : the problem is that there is a conflict of script and I can't trigger my script (put on this very page). How can I force it to make my java script listened first ?
In fact the checkbox are constructed by myjs.js, applying to the html sequece
<div class="left">
<input type="radio" name="isPubOk" id="pubOk" checked="checked" />
<label for="pubOk"><?php echo _("Oui"); ?></label>
</div>
<div class="left">
<input type ="radio" name="isPubNok" id="pubNok" checked="" />
<label for="pubNok"><?php echo _("Non"); ?></label>
</div>
Here's a sample of the js file :
function initCustomForms() {
getElements();
separateElements();
replaceRadios();
replaceCheckboxes();
replaceSelects();
// hide drop when scrolling or resizing window
if (window.addEventListener) {
window.addEventListener("scroll", hideActiveSelectDrop, false);
window.addEventListener("resize", hideActiveSelectDrop, false);
}
else if (window.attachEvent) {
window.attachEvent("onscroll", hideActiveSelectDrop);
window.attachEvent("onresize", hideActiveSelectDrop);
}
}
function refreshCustomForms() {
// remove prevously created elements
if(window.inputs) {
for(var i = 0; i < checkboxes.length; i++) {
if(checkboxes[i].checked) {checkboxes[i]._ca.className = "checkboxAreaChecked";}
else {checkboxes[i]._ca.className = "checkboxArea";}
}
for(var i = 0; i < radios.length; i++) {
if(radios[i].checked) {radios[i]._ra.className = "radioAreaChecked";}
else {radios[i]._ra.className = "radioArea";}
}
for(var i = 0; i < selects.length; i++) {
var newText = document.createElement('div');
if (selects[i].options[selects[i].selectedIndex].title.indexOf('image') != -1) {
newText.innerHTML = '<img src="'+selects[i].options[selects[i].selectedIndex].title+'" alt="" />';
newText.innerHTML += '<span>'+selects[i].options[selects[i].selectedIndex].text+'</span>';
} else {
newText.innerHTML = selects[i].options[selects[i].selectedIndex].text;
}
document.getElementById("mySelectText"+i).innerHTML = newText.innerHTML;
}
}
}
// getting all the required elements
function getElements() {
// remove prevously created elements
if(window.inputs) {
for(var i = 0; i < inputs.length; i++) {
inputs[i].className = inputs[i].className.replace('outtaHere','');
if(inputs[i]._ca) inputs[i]._ca.parentNode.removeChild(inputs[i]._ca);
else if(inputs[i]._ra) inputs[i]._ra.parentNode.removeChild(inputs[i]._ra);
}
for(i = 0; i < selects.length; i++) {
selects[i].replaced = null;
selects[i].className = selects[i].className.replace('outtaHere','');
selects[i]._optionsDiv._parent.parentNode.removeChild(selects[i]._optionsDiv._parent);
selects[i]._optionsDiv.parentNode.removeChild(selects[i]._optionsDiv);
}
}
// reset state
inputs = new Array();
selects = new Array();
labels = new Array();
radios = new Array();
radioLabels = new Array();
checkboxes = new Array();
checkboxLabels = new Array();
for (var nf = 0; nf < document.getElementsByTagName("form").length; nf++) {
if(document.forms[nf].className.indexOf("default") < 0) {
for(var nfi = 0; nfi < document.forms[nf].getElementsByTagName("input").length; nfi++) {inputs.push(document.forms[nf].getElementsByTagName("input")[nfi]);
}
for(var nfl = 0; nfl < document.forms[nf].getElementsByTagName("label").length; nfl++) {labels.push(document.forms[nf].getElementsByTagName("label")[nfl]);}
for(var nfs = 0; nfs < document.forms[nf].getElementsByTagName("select").length; nfs++) {selects.push(document.forms[nf].getElementsByTagName("select")[nfs]);}
}
}
}
// separating all the elements in their respective arrays
function separateElements() {
var r = 0; var c = 0; var t = 0; var rl = 0; var cl = 0; var tl = 0; var b = 0;
for (var q = 0; q < inputs.length; q++) {
if(inputs[q].type == "radio") {
radios[r] = inputs[q]; ++r;
for(var w = 0; w < labels.length; w++) {
if((inputs[q].id) && labels[w].htmlFor == inputs[q].id)
{
radioLabels[rl] = labels[w];
++rl;
}
}
}
if(inputs[q].type == "checkbox") {
checkboxes[c] = inputs[q]; ++c;
for(var w = 0; w < labels.length; w++) {
if((inputs[q].id) && (labels[w].htmlFor == inputs[q].id))
{
checkboxLabels[cl] = labels[w];
++cl;
}
}
}
}
}
//replacing radio buttons
function replaceRadios() {
for (var q = 0; q < radios.length; q++) {
radios[q].className += " outtaHere";
var radioArea = document.createElement("div");
if(radios[q].checked) {
radioArea.className = "radioAreaChecked";
}
else
{
radioArea.className = "radioArea";
}
radioArea.id = "myRadio" + q;
radios[q].parentNode.insertBefore(radioArea, radios[q]);
radios[q]._ra = radioArea;
radioArea.onclick = new Function('rechangeRadios('+q+')');
if (radioLabels[q]) {
if(radios[q].checked) {
radioLabels[q].className += "radioAreaCheckedLabel";
}
radioLabels[q].onclick = new Function('rechangeRadios('+q+')');
}
}
return true;
}
//checking radios
function checkRadios(who) {
var what = radios[who]._ra;
for(var q = 0; q < radios.length; q++) {
if((radios[q]._ra.className == "radioAreaChecked") && (radios[q]._ra.nextSibling.name == radios[who].name))
{
radios[q]._ra.className = "radioArea";
}
}
what.className = "radioAreaChecked";
}
//changing radios
function changeRadios(who) {
if(radios[who].checked) {
for(var q = 0; q < radios.length; q++) {
if(radios[q].name == radios[who].name) {
radios[q].checked = false;
}
radios[who].checked = true;
checkRadios(who);
}
}
}
//rechanging radios
function rechangeRadios(who) {
if(!radios[who].checked) {
for(var q = 0; q < radios.length; q++) {
if(radios[q].name == radios[who].name) {
radios[q].checked = false;
}
if(radioLabels[q]) {
radioLabels[q].className = radioLabels[q].className.replace("radioAreaCheckedLabel","");
}
}
radios[who].checked = true;
if(radioLabels[who] && radioLabels[who].className.indexOf("radioAreaCheckedLabel") < 0) {
radioLabels[who].className += " radioAreaCheckedLabel";
}
checkRadios(who);
if(window.$ && window.$.fn) {
$(radios[who]).trigger('change');
}
}
}
//replacing checkboxes
function replaceCheckboxes() {
if (replaceCheckBoxes == 0)
return;
for (var q = 0; q < checkboxes.length; q++) {
// checkboxes[q].className += " outtaHere";
var checkboxArea = document.createElement("div");
if(checkboxes[q].checked) {
checkboxArea.className = "checkboxAreaChecked";
if(checkboxLabels[q]) {
checkboxLabels[q].className += " checkboxAreaCheckedLabel"
}
}
else {
checkboxArea.className = "checkboxArea";
}
checkboxArea.id = "myCheckbox" + q;
checkboxes[q].parentNode.insertBefore(checkboxArea, checkboxes[q]);
checkboxes[q]._ca = checkboxArea;
checkboxArea.onclick = new Function('rechangeCheckboxes('+q+')');
if (checkboxLabels[q]) {
checkboxLabels[q].onclick = new Function('changeCheckboxes('+q+')');
}
checkboxes[q].onkeydown = checkEvent;
}
return true;
}
//checking checkboxes
function checkCheckboxes(who, action) {
var what = checkboxes[who]._ca;
if(action == true) {
what.className = "checkboxAreaChecked";
what.checked = true;
}
if(action == false) {
what.className = "checkboxArea";
what.checked = false;
}
if(checkboxLabels[who]) {
if(checkboxes[who].checked) {
if(checkboxLabels[who].className.indexOf("checkboxAreaCheckedLabel") < 0) {
checkboxLabels[who].className += " checkboxAreaCheckedLabel";
}
} else {
checkboxLabels[who].className = checkboxLabels[who].className.replace("checkboxAreaCheckedLabel", "");
}
}
}
//changing checkboxes
function changeCheckboxes(who) {
setTimeout(function(){
if(checkboxes[who].checked) {
checkCheckboxes(who, true);
} else {
checkCheckboxes(who, false);
}
},10);
}

Please see the jquery stopImmediatePropagation() function here: http://docs.jquery.com/Types/Event#event.stopImmediatePropagation.28.29
I believe this will achieve what you are looking to do.
Edit: With more detail I may be able to provide a better answer.
Edit 2: It appears that there is no guarantee'd order of execution in Javascript, so inline code may not run before dynamically added code. In addition this particular function may only work if the other handlers are added using jQuery.
Edit 3:
A quick and dirty fix would be to add
<script type="text/javascript">var executeHandlers = false;</script>
to the top of the one html file.
Then edit the javascript file such that the event handlers have
if (executeHandlers !== false) { ... do the logic you normally would here ... }
as the body
This would add one line to the html file that needs to be treated differently, and should not impact the execution on the other pages.
Please note that this is a quick and dirty fix, and there are better ways to do this. Working with the constraints of an existing .js file, and only one file that needs to be treated differently, this seems to be the fastest / easiest way to the desired outcome, not necessarily the best.

Related

window.location.reload(); stuck in infinite loop

I want this function to loop through all items, if it finds the right item to load its page, and if it doesn't find the right item it should reload the page and for loop again. When I delete the window.location.reload(); it loads normally to the item page. This is the code:
var item_name = "Washed";
var item_color = "Red";
function pickItem() {
let items = document.getElementsByClassName("name-link");
for(i = 0; i < items.length; i++) {
if((items[i].innerHTML).includes(item_name)) {
for(j = 0; j < items.length; j++) {
if((items[j].innerHTML).includes(item_color)) {
if(items[i].href == items[j].href) {
window.location.assign(items[i, j].href);
}
}
}
}
}
window.location.reload();
}
In the following form it works as I want, but why does it need the chrome.storage function to work?(I used it with the chrome.storage before, but it was too slow for my purposes so I had to change it.)
var item_name = "Washed";
var item_color = "Red";
function pickItem() {
let items = document.getElementsByClassName("name-link");
chrome.storage.sync.get(["itemName", "color"], function(data) {
for(i = 0; i < items.length; i++) {
if((items[i].innerHTML).includes(item_name)) {
for(j = 0; j < items.length; j++) {
if((items[j].innerHTML).includes(item_color)) {
if(items[i].href == items[j].href) {
window.location.assign(items[i, j].href);
chrome.storage.sync.set({"item_url": items[i, j].href});
}
}
}
}
}
})
window.location.reload()
}
I'd recommend adding a boolean variable which indicates if a location to navigate to has been found yet and wrap the call to window.location.reload() inside an if-block which checks the state of the variable.
e.g.
function pickItem() {
let items = document.getElementsByClassName("name-link");
let found = false;
for (i = 0; i < items.length; i++) {
if ((items[i].innerHTML).includes(item_name)) {
for (j = 0; j < items.length; j++) {
if ((items[j].innerHTML).includes(item_color)) {
if (items[i].href == items[j].href) {
found = true;
window.location.assign(items[i, j].href);
}
}
}
}
}
if (!found) {
window.location.reload();
}
}

White page with loop loading in firefox

I have a problem only in Firefox. When I click a button in my application in Firefox the page is white with infinite loading, it's in a loop.
I have done a debugger on my code with dev console and I found where the function breaks. Note that there are no errors in the console.
function setSavedToSended() {
console.log('sono nella funzione')
if (projObjSave == undefined) {
projObjSave = projObj;
}
var items = $("td [id-status]"); //the problem starts here
for (var item in items) {
var _this = parseInt($(items[item] /* here it breaks item*/ ).attr("id-status"));
if (_this == 1) {
var tempId = $(items[item]).attr("id")
$("#" + tempId).attr("id-status", 2).addClass("sended");
var currProj = tempId.split("_");
var projIndex, taskIndex;
for (var i = 0; i < projObj.length; i++) {
if (currProj[0] == projObj[i].projectId) {
projIndex = i;
}
}
if (currProj.length == 3) {
if (currProj[1] == "ORD") {
projObjSave[projIndex].timeTrackingProjectList.timeTrackingOrdinario[currProj[2]]["idStatus"] = 2;
} else {
projObjSave[projIndex].timeTrackingProjectList.timeTrackingStraordinario[currProj[2]]["idStatus"] = 2;
}
} else {
for (var index = 0; index < projObj[projIndex].taskList.length; index++) {
if (currProj[1] == projObj[projIndex].taskList[index].taskId) {
taskIndex = index;
}
}
currProj[2] == "ORD" ? projObjSave[projIndex].taskList[taskIndex].timeTrackingTaskOrdinario[currProj[3]]["idStatus"] = 2 : projObjSave[projIndex].taskList[taskIndex].timeTrackingTaskStraordinario[currProj[3]]["idStatus"] = 2;
}
} else console.log('nothing done')
}
saveSendProject(2)
}

Need to split up a quiz written in javascript into 1 page per Q instead of all Qs on a single page

I have a quiz currently all on one page written in javascript. I need to rewrite/modify/split up this code so each question appears on its own page.
I used a tool called eXeLearning to build the quiz. This is part of a SCORM project that will be hosted on an LMS. I'm just not sure how to go about this. I'm good with HTML and CSS and I know a little PHP, but not javascript. Here is the script in question, I can also post the entire page of code if need be:
<script type="text/javascript">
<!-- //<![CDATA[
var numQuestions = 10;
var rawScore = 0;
var actualScore = 0;
var question0;
var question1;
var question2;
var question3;
var question4;
var question5;
var question6;
var question7;
var question8;
var question9;
var key0 = 0;
var key1 = 1;
var key2 = 0;
var key3 = 0;
var key4 = 1;
var key5 = 0;
var key6 = 0;
var key7 = 1;
var key8 = 0;
var key9 = 1;
function getAnswer()
{
scorm.SetInteractionValue("cmi.interactions.0.id","key0b0");
scorm.SetInteractionValue("cmi.interactions.0.type","choice");
scorm.SetInteractionValue("cmi.interactions.0.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key0b0[i].checked)
{
question0 = document.getElementById("quizForm0").key0b0[i].value;
scorm.SetInteractionValue("cmi.interactions.0.student_response",question0);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.1.id","key1b0");
scorm.SetInteractionValue("cmi.interactions.1.type","choice");
scorm.SetInteractionValue("cmi.interactions.1.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key1b0[i].checked)
{
question1 = document.getElementById("quizForm0").key1b0[i].value;
scorm.SetInteractionValue("cmi.interactions.1.student_response",question1);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.2.id","key2b0");
scorm.SetInteractionValue("cmi.interactions.2.type","choice");
scorm.SetInteractionValue("cmi.interactions.2.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key2b0[i].checked)
{
question2 = document.getElementById("quizForm0").key2b0[i].value;
scorm.SetInteractionValue("cmi.interactions.2.student_response",question2);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.3.id","key3b0");
scorm.SetInteractionValue("cmi.interactions.3.type","choice");
scorm.SetInteractionValue("cmi.interactions.3.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key3b0[i].checked)
{
question3 = document.getElementById("quizForm0").key3b0[i].value;
scorm.SetInteractionValue("cmi.interactions.3.student_response",question3);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.4.id","key4b0");
scorm.SetInteractionValue("cmi.interactions.4.type","choice");
scorm.SetInteractionValue("cmi.interactions.4.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key4b0[i].checked)
{
question4 = document.getElementById("quizForm0").key4b0[i].value;
scorm.SetInteractionValue("cmi.interactions.4.student_response",question4);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.5.id","key5b0");
scorm.SetInteractionValue("cmi.interactions.5.type","choice");
scorm.SetInteractionValue("cmi.interactions.5.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key5b0[i].checked)
{
question5 = document.getElementById("quizForm0").key5b0[i].value;
scorm.SetInteractionValue("cmi.interactions.5.student_response",question5);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.6.id","key6b0");
scorm.SetInteractionValue("cmi.interactions.6.type","choice");
scorm.SetInteractionValue("cmi.interactions.6.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key6b0[i].checked)
{
question6 = document.getElementById("quizForm0").key6b0[i].value;
scorm.SetInteractionValue("cmi.interactions.6.student_response",question6);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.7.id","key7b0");
scorm.SetInteractionValue("cmi.interactions.7.type","choice");
scorm.SetInteractionValue("cmi.interactions.7.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key7b0[i].checked)
{
question7 = document.getElementById("quizForm0").key7b0[i].value;
scorm.SetInteractionValue("cmi.interactions.7.student_response",question7);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.8.id","key8b0");
scorm.SetInteractionValue("cmi.interactions.8.type","choice");
scorm.SetInteractionValue("cmi.interactions.8.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key8b0[i].checked)
{
question8 = document.getElementById("quizForm0").key8b0[i].value;
scorm.SetInteractionValue("cmi.interactions.8.student_response",question8);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.9.id","key9b0");
scorm.SetInteractionValue("cmi.interactions.9.type","choice");
scorm.SetInteractionValue("cmi.interactions.9.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key9b0[i].checked)
{
question9 = document.getElementById("quizForm0").key9b0[i].value;
scorm.SetInteractionValue("cmi.interactions.9.student_response",question9);
break;
}
}
}
function calcRawScore(){
if (question0 == key0)
{
scorm.SetInteractionValue("cmi.interactions.0.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.0.result","wrong");
}
if (question1 == key1)
{
scorm.SetInteractionValue("cmi.interactions.1.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.1.result","wrong");
}
if (question2 == key2)
{
scorm.SetInteractionValue("cmi.interactions.2.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.2.result","wrong");
}
if (question3 == key3)
{
scorm.SetInteractionValue("cmi.interactions.3.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.3.result","wrong");
}
if (question4 == key4)
{
scorm.SetInteractionValue("cmi.interactions.4.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.4.result","wrong");
}
if (question5 == key5)
{
scorm.SetInteractionValue("cmi.interactions.5.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.5.result","wrong");
}
if (question6 == key6)
{
scorm.SetInteractionValue("cmi.interactions.6.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.6.result","wrong");
}
if (question7 == key7)
{
scorm.SetInteractionValue("cmi.interactions.7.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.7.result","wrong");
}
if (question8 == key8)
{
scorm.SetInteractionValue("cmi.interactions.8.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.8.result","wrong");
}
if (question9 == key9)
{
scorm.SetInteractionValue("cmi.interactions.9.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.9.result","wrong");
}
}
function calcScore2()
{
computeTime(); // the student has stopped here.
document.getElementById("quizForm0").submitB.disabled = true;
getAnswer();
calcRawScore();
actualScore = Math.round(rawScore / numQuestions * 100);
alert("Your score is " + actualScore + "%")
scorm.SetScoreRaw(actualScore+"" );
scorm.SetScoreMax("100");
var mode = scorm.GetMode();
if ( mode != "review" && mode != "browse" ){
if ( actualScore < 80 )
{
scorm.SetCompletionStatus("incomplete");
scorm.SetSuccessStatus("failed");
}
else
{
scorm.SetCompletionStatus("completed");
scorm.SetSuccessStatus("passed");
}
scorm.SetExit("");
}
exitPageStatus = true;
scorm.save();
scorm.quit();
}
//]]> -->
</script>
This is a very broad question and you will certainly need to learn JavaScript (or hire somebody) in order to solve this. I also recommend learning JQuery especially if you go with option 1 below. There's lots of sites that will teach you.
In terms of the general approach you can take, you have two choices:
Keep everything on one page and show/hide quiz questions as the learner progresses through.
Put questions in frames and pass data back to the parent frame.
I'd use option 1 myself because frames are quite old fashioned now.
Pick an approach, take some time to learn JavaScript and JQuery and then come back with more specific questions about problems you run into implementing the approach you choose.

avoid sorting in the JSP pages

var sortitems = 1;
function move(fbox, tbox, all)
{
for ( var i = 0; i < fbox.options.length; i++)
{
if (!all && fbox.options[i].selected && fbox.options[i].value != "")
{
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
else
{
if (all && fbox.options[i].value != "")
{
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
}
}
BumpUp(fbox);
if (sortitems)
SortD(tbox);
checkSelectAll();
}
This move function is getting called after clicking on the button, then it will call the sort method where sorting is happening by alphabetically. So we dont need to sort we need to populate the data as it is from the left side box to right side box and vice versa, but sorting is happening. Please help out be here.
function SortD(box)
{
var temp_opts = new Array();
var temp = new Object();
for ( var i = 0; i < box.options.length; i++)
{
temp_opts[i] = box.options[i];
}
for ( var x = 0; x < temp_opts.length - 1; x++)
{
for ( var y = (x + 1); y < temp_opts.length; y++)
{
if (temp_opts[x].value > temp_opts[y].value)
{
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
}
}
}
for ( var i = 0; i < box.options.length; i++)
{
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
}
}
Depends on the bumpup box function. The elements are moving from one box to another. It will replace the element with empty space and move to top and do for all the elements. Please help out me here
Thanks in advance
function BumpUp(box)
{
for ( var i = 0; i < box.options.length; i++)
{
if (box.options[i].value == "")
{
for ( var j = i; j < box.options.length - 1; j++)
{
box.options[j].value = box.options[j + 1].value;
box.options[j].text = box.options[j + 1].text;
}
var ln = i;
break;
}
}
if (ln < box.options.length)
{
box.options.length -= 1;
BumpUp(box);
}
}
Maybe it's just me, but it's hard to see what the issue is here.
If it is simply that SortD(tbox) is being called within the move() function, that's because
sortitems is set to 1 right at the top of the code. The value of sortitems is never changed anywhere else, so this conditional is always true and SortD is always called.
if (sortitems)
SortD(tbox);

remove null value in javascript

am having script with working condition but many for loop is there so any way to do this...to simplify this ...am new to script kindly help on this....
function change()
{
//document.getElementById("Geography").options[7]=new Option("", "newval", true, false);
var geo = document.getElementById("Geography").options;
var zon = document.getElementById("zone").options;
var coun = document.getElementById("country").options;
for (var i = 0; i < geo.length; i++)
{
if (geo[i].innerHTML == "Null Value" || geo[i].innerHTML == "")
{
document.getElementById("Geography").options[i] = null;
}
}
for (var i = 0; i < coun.length; i++)
{
alert("Loop1" + i);
if (coun[i].innerHTML == "Null Value")
{
document.getElementById("country").options[i] = null;
}
}
for (var i = 0; i < zon.length; i++)
{
//alert("Loop1" + i);
if (zon[i].innerHTML == "Null Value")
{
document.getElementById("zone").options[i] = null;
}
}
}
To remove an option, call removeChild() on the parent element.
var geoSel = document.getElementById("Geography");
var geo = geoSel.options;
for (var i = geoSel.options.length-1; i >= 0; i--) {
if (geo[i].innerHTML == "Null Value" || geo[i].innerHTML == "") {
geo.removeChild(geo[i]);
}
}
I count down instead of up because removing a child will cause the indexes of all the following children to be shifted down. In a count-up loop, that will cause elements to be skipped.
use this UPDATED DEMO
function change(){
var optionsArr = [];
optionsArr.push(document.getElementById("Geography").options);
optionsArr.push(document.getElementById("zone").options);
optionsArr.push(document.getElementById("country").options);
var optArrlenght = optionsArr.length;
for ( var j = 0; j < optArrlenght; j++){
var options = optionsArr[j];
var optionslength = options.length;
for (var i = 0; i < optionslength; i++)
{
if (options[i].innerHTML == "Null Value" || options[i].innerHTML == "")
{
options[i].remove();
i--;
optionslength--;
}
}
}
}
change();

Categories

Resources