Trouble converting Javascript array into JQuery for IE7 compatibility - javascript

I am trying to convert a line in Javascript so that the functionality works in IE7. Below is the code I am trying to convert. Obviously IE doesn't like the class attribute being set which is where the issue occurs. So my solution was to translate it using JQuery for browser compatibility. But I am having trouble converting the array into something JQuery understands.
This is the code:
ulAccNm.children[iVal].setAttribute("class", "show");
I have tried the following, but it doesn't work correctly.
$(ulAccNm).children(jQuery.inArray("iVal")).addClass('show').removeClass('hide');
The variable of iVal translates into the total number of children to the parent and loops through each to determine to add a class or not. Any help would be greatly appreciated.

I'm guessing a lot here, but I think this will translate:
$($(ulAccNm).children()[iVal]).addClass('show').removeClass('hide');
However, if you want to do this to ALL the children, you don't need an explicit loop with iVal. Just use
$(ulAccNm).children().addClass('show').removeClass('hide');

You may just want to try changing your loop structure from a for loop to the jQuery.each method:
$(ulAccNm).children().each(function () {
// Decide if the class needs to be changed
if (needsToBeChanged)
$(this).addClass('show').removeClass('hide');
})

Ended up fixing by using Kevin B's suggestion of .children().eq(ival) and this makes it work

Related

Format integer thousands separator throughout the document

After months of web-development, I find myself completely helpless trying to find a good solution for a simple problem of formatting all the numbers throughout the DOM as I wish. Specifically, I have a js function my_int_formatter(), that I want to apply to all integers after the doc has been loaded. Best descriped by example - I want to do something like
<td>my_int_formatter({{django_variable}})</td>
I know the code above won't work, because I have to include 'script' tag, but first, I don't like the messy code, and second, javascript won't recognize python variable
I tried the following way:
HTML
<td class = 'my_integer'>{{django_variable}}</td>
JS
$(document).ready(function(){
// ....
content = $('.my_integer').html();
$('.my_integer').html(my_int_formatter(content));
...but as expected, I got wrong results because the js code applied the same html() content of the first .my_integer element in the DOM chain to all the others. Any ideas how to do this the short and correct way ?
If I understand correctly, your problem isn't with the formatting but actualy applying the formatting to each of your dom elements.
Try using jquerys .each() function and using $(this).html() to actualy grab the content.
$('.my_integer').each(function(){
content = $(this).html();
$(this).html(content+"formatted");
});
here's a quick fiddle :
https://jsfiddle.net/57rdq2a0/2/
If I understand you correctly, you want to use builtin django.contrib.humanize application: https://docs.djangoproject.com/en/1.9/ref/contrib/humanize/
You can format integers using some predefined filters, for example intcomma:
4500 becomes 4,500.
45000 becomes 45,000.
450000 becomes 450,000.
4500000 becomes 4,500,000.
Usage in your case would be like
{% load humanize %}
<td>{{django_variable|intcomma}}</td>
Also don't forget to include the app in INSTALLED_APPS
Also this question might be useful
If you want to apply filter to all variables of some kind, I suggest you to use Middleware to fiddle with response before rendering.

Javascript/HTML assigning a value

I am having a problem with some Javascript/HTML/CSS code. I am fairly new to creating websites, so please bear with me.
what I am ultimately trying to do is pull a dynamic value from javascript and use that to sort some divs (in a container). What I am thinking is I will assign the value to the Id and then pull those into an array to be sorted by tinysort. If there's a faster way to do this, let me know.
However, my first problem is putting the data into the id so it can be sorted. Would I do something like
document.getElementById(namesort).value = iterator;
or would I use something like myData?
Note: I don't want to display the value, I just want to use it to sort.
Please ask for clarification if needed! Thanks in advance. :)
Here is the applicable code to this problem. http://jsfiddle.net/dw77hLyp/1/
It just basically shows a very basic outline of some of my code.
Without JQuery. You can create your attribute :
document.getElementById(namesort).createAttribute('myData');
document.getElementById(namesort).setAttribute("myData","hello Im in");
Pure JS Solution:
for( i = 0 ; i < iterator.length ; i++ ) {
document.getElementsByName('namesort')[i].setAttribute('data-sort',iterator[i]);
}
That way you add each of those elements an attribute called data-sort where the iterator as a value, then could create an array insert all the namesort elements to it, use the sorting algorithm and you're done.
document.getElementById("divID").innerHTML="someContent";

JavaScript to jQuery for KendoUI

I've been using KendoUI and have been using they're command functions. However to call JS I must call named jS functions. No huge deal. When I use the "This" key word it brings back the entire grid and I mus find a value of a child from a sibling of the same parent elements and i wound up doing this ugly thing. The question I have is how can I turn this "thing" into something jqueryable readable and comprehensible
function AddRole(e) {
var $ParentNode = e.target.parentNode.parentNode.children[1].children[0].getAttribute("value", 0);
}
Sorry, but you have other problems.
If you rely on such a structure e.target.parentNode.parentNode.children[1].children[0], your Markup and JS do not scale at all.
Use the oppurtunity to create scalable and consistent code. Or at least, set some id, class or html5 data attribute on the children[0] element in order to identify it properly.

jQuery: is element.click(func) or element.attr('onclick','func()') more efficient?

I'm populating a list by cloning elements into it. Then I change attrs to make each item unique. They need to call a function on click, so I'm wondering if it's more efficient to use new_element.click(func); or new_element.attr('onlick','func();');
new_element.attr('onclick','func();');
Is:
inefficient (needlessly creating a new inline function from a string, that does nothing except call func and lose the this reference);
aggravating to put any complex code in, since it all has to be JS string escaped;
broken in IE, due to bugs in setAttribute.
Avoid. click()/bind('click') is there for a reason.
onclick has a number of limitations, including cluttering the DOM and only allowing one function at a time. So you should use click. See Quirks Mode for more information.
Directly referencing the function will be more efficient than having to interpret a string.
The lowest touch way of doing this, however, is this way:
$(links_selector).live('click', func);
links_selector will presumably be something like ul.listClass a.actionClass. This will not require anything to be done when new list elements get added.
Since you are using jQuery then make it this way
new_element.click(function(){
// your code
});
or you can bind the click event handler like
new_element.bind("click", function(){
// your code
});
Any difference in performance between the two is most likely going to be negligible. You should use the one that reads better, and that's element.click. (Plus, onclick has many disadvantages, as #Matthew Flaschen mentioned.)

How to increase speed of getElementById() function on IE or give other solutions?

I have a project using Javascript parse json string and put data into div content.
In this case, all of itemname variables is the same div's id.
If variable i about 900, I run this code in Firefox 3 for <10ms, but it run on IE 7 for >9s, IE process this code slower 100 times than Firefox
I don't know what happen with IE ?
If I remove the line document.getElementById(itemname), speed of them seems the same.
The main problem arcording to me is document.getElementById() function?
Could you show me how to solve this prolem to increase this code on IE ?
Thank in advance.
var i = obj.items.length-2;
hnxmessageid = obj.items[i+1].v;
do{
itemname = obj.items[i].n;
itemvalue = obj.items[i].v;
document.getElementByid(itemname);
i--;
}while(i>=0);
Are you really noticing any latency?
gEBI is natively very very fast, I don't think you can avoid it anyway for what you're doing. Could you provide a low-down of what you're doing precisely? It looks like you're using a loop, but can you post exactly what you're doing inside of the loop, what your common goal of the script is?
document.getElementByid(itemname) is the fastest way to get a single element from the DOM in any real application you will sould not see any problems with using it, if you do see a problem you need to rethink you code a little it possible to acomplish any task with just a handfull of calls for this method. You can present you full problem if you like so I could show you an example
At least cache the reference to document:
var doc = document;
for(;;) {
doc.getElementById(..);
}

Categories

Resources