issues with javascript checkboxes issues - javascript

the following code just says unexpected identifier;
var icheckBoxes = $('input[type="checkbox"]');
for(int i=0; i < icheckBoxes.length; i++) {
if (!icheckBoxes[i].is(":checked")) {
$('input[type="checkbox').addClass('has-error');
AgreeIsActive = false;
$('#AgreementSubmit').prop('disabled', false);
return;
}
}
Seems to be the int as the culprit but i tried it to change it to var but no success and got a different error:
Uncaught TypeError: icheckBoxes[i].is is not a function
Update#1
New Code added with the help
var icheckBoxes = $('input[type="checkbox"]');
alert(icheckBoxes.length);
for(var i=0; i < icheckBoxes.length; i++) {
alert(i);
//if (!icheckBoxes[i].is(":checked")) {
if(!icheckBoxes.eq(i).is(":checked")) {
alert(i);
$('input[type="checkbox').addClass('has-error');
AgreeIsActive = false;
$('#AgreementSubmit').prop('disabled', false);
return;
}
}
it says the length is 4 and then it goes inside and it alerts 4 times 0 and the next alert which is inside the if condition, it just alerts it 1 time and that is also 0

icheckBoxes[i].is(":checked")
should be
icheckBoxes.eq(i).is(":checked")
[] breaks the element out of the jQuery object and is() is a jQuery method.
Also as a side note, $('input[type="checkbox') is missing the "] on the end of it to make it a valid selector.

Related

Object doesn't support property or method 'item' IE 9

I am trying to find all buttonitems. In chrome and modern browsers this works fine but in ie9 I am having the error above. I can't change the HTML on the page only the javascript on the page. In chrome I can use document.getElementsByClassName() but in ie 9 I can't even though I should be able to probably because the website has to run in comparability with Quirks. I have tried rewriting the code to work without this and believe I have got the elements with the code
if(navigator.userAgent.toLowerCase().indexOf('msie') != -1){
if (document.all) {
var allElements = document.all;
} else {
var allElements = document.getElementsByTagName("*");
}
// Empty placeholder to put in the found elements with the class name
var oldHrefs = [];
for (var i = 0, ii = allElements.length; i < ii; i++) {
if (allElements[i].className == 'ButtonItem') {
oldHrefs[oldHrefs.length] = allElements[i];
}
}
}
else
{
var oldHrefs = document.getElementsByClassName('ButtonItem');
}
But I then need to loop through the code and remove the href and add an onclick which is when I revive my next error. Object doesn't support property or method 'item'. Any ideas what I can do to correct the code below?
for (var i = 0; i < oldHrefs.length; i++) {
var thisHref = oldHrefs.item(i).getAttribute('href');
if (thisHref != null) {
if (thisHref.includes('Act=1468')) {
oldHrefs.item(i).removeAttribute("href");
oldHrefs.item(i).setAttribute('onclick', 'window.location.href="' + thisHref + '";this.removeAttribute("onclick");');
}
}
}
Thanks

Getting the error: Failed to execute 'appendChild' on 'Node'

Am using pure javascript prototype functions to create a list of checkbox and append them as we iterate over the data. Everything is created using javascript and css.
When I run it so it can draw a list of checkboxes, it throws me an error :
"Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'."
for this line:
results.push(this.list.appendChild(letter.draw));
Am sure there is nothing wrong with checkboxinput prototype since it works fine everywhere else. There is something wrong am doing in FormTicket.Can anyone help me what am doing wrong?
The form page is FormTicket
FormTicket.prototype.respond = function(data) {
var letter, i, len;
this.letters = [];
for (i = 0, len = data.length; i < len; i++) {
letter = data[i];
this.letters.push(new ParaInfo({
InfoIndividual: letter
}));
}
return this.drawList();
};
FormTicket.prototype.drawList = function() {
var letter, i, len, ref, results;
ref = this.letters;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
letter = ref[i];
results.push(this.list.appendChild(letter.draw));
}
return results;
};
The draw function in the ParaInfo method is:
ParaInfo.prototype.draw = function() {
this.e = new CheckBoxInput({
title: this.InfoIndividual,
float: 'left',
});
return this.e;
};
and just FYI, the checkboxinput is another prototype, part of it as below:
CheckBoxInput.prototype.build = function(arg) {
this.title = arg.title;
this.float = arg.float;
};
CheckBoxInput.prototype.draw = function() {
var box, check;
this.item = document.createElement('div').addClass('checkboxInput');
return this.item;
};
Solved it. needed to call ".draw().draw()"
i.e Initially was calling .draw() of ParaInfo only. Added another ".draw()" which calls the draw of CheckBoxInput. Correct call for append:
results.push(this.list.appendChild(letter.draw().draw()));
Thanks to user apsillers for his valuable input as well.

getOptions() for loop not working

The following code throws error. I try to get (alert) the value and options of an Optionset in MS CRM 2013, It successfully shows all the things but after that it shows error. i attached the screen shot that error
function GetOptionsetLable()
{
var OptionSetControl = Xrm.Page.getAttribute("test_613a");
for(var intCounter=0; OptionSetControl .getOptions().length; intCounter++)
{
var backendvalue=OptionSetControl .getOptions()[intCounter].value;
alert(backendvalue.toString());
}
}
function GetOptionsetLable()
{
var OptionSetControl = Xrm.Page.getAttribute("test_613a");
for(var intCounter=0; OptionSetControl .getOptions().length; intCounter++)
{
var backendvalue=OptionSetControl .getOptions()[intCounter].value;
alert(backendvalue.toString());
}
}
Your for loop will never end because you don't tell it when to stop.
OptionSetControl.getOptions().length
should be:
intCounter < OptionSetControl.getOptions().length
Full code:
function GetOptionsetLable()
{
var OptionSetControl = Xrm.Page.getAttribute("test_613a");
for(var intCounter=0; intCounter < OptionSetControl.getOptions().length; intCounter++)
{
var backendvalue=OptionSetControl.getOptions()[intCounter].value;
alert(backendvalue.toString());
}
}
Remember that the value property contains the optionset numeric value, and the .text property the label.
you can also use a shorter for condition:
var options = Xrm.Page.getAttribute("test_613a").getOptions();
for (var i in options) {
alert(options[i].value);
}
I also created a library, you can find here:
OptionSet JavaScript Helper Library

Function code after for loop doesn't get executed ever

I'm trying to create a registration form and validate input fields using javascript, but I'm having difficulties after the for loop executes...
function checkValidation1() {
var elem = document.getElementsByTagName("input");
for(var i = 0; i < elem.length; i++) {
if($("#"+elem[i].id).val()=="") {
$("#"+elem[i].id+"error").html("<img src='images/exclamationsmall.png' title='"+elem[i].name+" should not be blank'>");
} else if(elem[i].name=="email") {
var emailReg = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*#([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/;
if(!emailReg.test($("#"+elem[i].id).val())) {
$("#"+elem[i].id+"error").html("<img src='images/exclamationsmall.png' title='"+elem[i].name+" is invalid'>");
}
} else {
$("#"+elem[i].id+"error").html("");
}
}
alert("fasfasfasfasfasf");
}
The alert doesn't execute for some reason. Any ideas?
Verify that all of your input elements actually have an id attribute. Otherwise this line:
if ($("#" + elem[i].id).val() == "") {
...will result in an expression containing only an octothorpe -- $("#") -- and the following error:
Syntax error, unrecognized expression: #
...which ultimately prevents the code from reaching the alert.

Chrome and Firefox incompatibility

I have two frames, the expression running in first frame and calling highlightElements function in another frame. This expression works fine in Firefox:
parent.frames[0].highlightElements(lineNumbers, stringObj);
The highlightElements function (just for sure):
function highlightElements(lineNumbers, stringObj) {
// run through the cycle and highlight them
//for (var ln in lineNumbers) {
var length = lineNumbers.length;
for (var ln=0; ln<length; ln++) {
var elements = $('.no');
//for (var i in elements) {
var el_length = elements.length;
for (var i=0; i<el_length; i++) {
if (parseInt(elements[i].innerHTML) == lineNumbers[ln]) {
var badThing = "yes";
for (var nextElement = elements[i].next();
nextElement.className != '.no'; nextElement = elements[i].next()) {
if (nextElement.innerHTML == stringObj) {
badThing = "no";
nextElement.effect('highlight', {}, 'slow');
scrollIntoView(nextElement);
}
}
if (badThing == "yes") alert("Didn't find the object");
}
}
}
}
But in Chrome it produces the error "Uncaught TypeError: Property 'highlightElement' of object[objectDOMWindow] is not a function".
How to change the expression to make it runnable in Chrome? Thanks
Make sure both frames are under same domain and protocol. Chome blocks javascript access from frames to another if the domains/protocols don't match. If you are working locally, and not under a local domain (i.e. the url is something like file:///C:/etc/etc.html) then it won't work either.

Categories

Resources