document.createElement in asp.net - javascript

What is the proper way to create a new client element through javascript in an asp.net page? Also, is it possible to move already created elements inside of the new element without causing too much havoc? Either straight javascript or jquery will work. Thanks.

I would recommend using jquery, because it abstracts out the browser inconsistencies. In jquery, you would use something like:
$('<div>').appendTo('#parent'); // parent is the id of the outer element
to create a new div element. You can use appendTo to add this element inside another dom element. Refer jquery documentation at http://docs.jquery.com/Main_Page

Related

Is it possible to add attribute to elements within shadow root?

so i'm using javascript and what i've done is capture a nodeList of shadowroot buttons within a webpage. I need to add attributes to each of them. Is there a way to do so? I've done it to other elements (not shadowroot) by using the .attr() but it appears shadow-roots act differently?
An example of a previously used code I had that is non shadow-root is the following:
elements.eq(i).attr("custom-attr", "analytics-purposes-only" +
elements.eq(i)..text().trim());
thank you in advanced.
also to note, this is my first time interacting with shadow-roots so things are new to me for this one.

Set variable to HTML elements with certain class?

I'm sure there is a way to set a JavaScript variable to any HTML element with a certain class attached to it? I'm just not sure how to write it. Can anyone help me out?
The best way would be to use a framework, such as jquery, that makes an easy use of (css) selectors, e.g, to select all elements with class my-class do
$('.my-class')
and then apply any code to the list of those elements
edit: don't forget to use the $(document).ready wrapper
you could simply go var x = document.getElementById('somediv'); for example. Now whatever style class pertains to somediv will of course still obtain unless you change it programatically in your javascript.
Hope that helps -- might be able to be more helpful if you give a broader context of what you are trying to accomplish.
To do this without document.getElementsByClassName('myclass') nor $(.'myclass') would be difficult and require some advanced tactics. You would have to recursively generate a list of all DOM objects from the document. While generating the list, every element touched would have to be tested for .className = 'myclass'. This is essentially what jquery does behind the scenes, although I believe on page load it caches the whole DOM for easier querying.

Do I need to use $(document).ready() when using $(document.createElement())?

I want to create a set of elements to add to a HTML document using JQuery's $(document.createElement()). I know $(document).ready() is required before starting using document elements.
However, is it necessary to use $(document).ready() in order to create elements with $(document.createElement())? In other word, can I use $(document.createElement()) in a document before it is ready?
You can create a new node at any time. But if you're going to be inserting it into the page's DOM, then you'll have to use .ready(), otherwise there's no guarantee that the spot you're trying to insert into exists yet.
I create new nodes before ready when I preload my images in the head... so It's not totally essential as a general rule or anything.

How we can find attach function in a html tag in javascript

Suppose we have a html tag and some javascript function is attached on that by addEvenetListner or attachEvent (with tag id or name but not inline), and if we need find out that which function is attach on that tag, then what is good way for find that.
Please suggest me.
If don't need to do it programmatically you can use visual event. If you need to do it programmatically i only have a jquery solution ($('#element').data('events');)
There is no native API to get event handler that was attached by addEventListener/attachEvent. jQuery store internal this handlers in some object. If you use jQuery you can done this by:
$('#someID').data('events');
Or you can write wrapper for addEventListener/attachEvent and store handlers manualy.

Access DOM element that is inserted after the page has loaded

My problem is basically quite simple.
After the page has been loaded some elements are added dynamically. I don't seem to be able to access those elements using normal jquery selectors.
Whenever I need to do this, I use livequery. Livequery, basically, allows you to bind events to elements even before they are in the DOM. Have a look, I'm pretty sure that's what you need! : )
maybe .live() could be helpful: http://api.jquery.com/live/

Categories

Resources