5

JavaScript: extracts the value of the div element

 2 years ago
source link: https://www.codesd.com/item/javascript-extracts-the-value-of-the-div-element.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JavaScript: extracts the value of the div element

advertisements

I have a html page containing something like:

<div data-variableid2="1234"> </div>

How can I get the 1234 value of an element (without jQuery) and show it with alert() ?


First you should give your DIV an id.

<div id="myid" data-variableid2="1234"> </div>

Then you could display the attributes data using

var MyDiv1 = document.getElementById('myid');
alert(MyDiv1.getAttribute('data-variableid2'));

Just test it here: http://jsfiddle.net/5C4NA/

But if you are not allowed to change the HTML of your pages, you should stick to something like

document.getElementsByTagName("div")[0]

but the index (here 0) does vary with the position of the div within the HTML page.

Edit 1:

With a loop you are able to scan through all DIVs and choose the one with an attribute data-variableid2: http://jsfiddle.net/5C4NA/1/ This looks like:

var elm = document.getElementsByTagName("div");
for (i=0;i<elm.length;i++) {
    if (elm[i].getAttribute('data-variableid2')!=null) {
        alert(elm[i].getAttribute('data-variableid2'));
    }
}

Related Articles

JavaScript removeChild does not remove the div element

I am facing a problem with JavaScript removeChild function and unfortunately its the first time I can't find what's wrong on the forum. Here is the scenario: -Create div elements with a for loop and put the word test in it -When one of those div is c

Using max-height on the DIV element

I have a DIV element that is set to contain a table element. The table element will have anywhere from 0 to upwards of 350 rows. I thought the easiest way to size this DIV element was to apply the max-height property so the DIV element would grow as

Get the class name in the div element by xpath

I want to get the class name inside the div element by xpath. <div class="indicator true"></div> And I want to check if the class nams equals to "indicator true". Any help?You can access the class value of the div element b

Hide the div element when the screen size is less than a specific size

I have a div element that I want to hide when the width of the browser is less than or equal to 1026px. Is this possible to do with the css: @media only screen and (min-width: 1140px) {} If it isn't possible with css, Is there any alternative? Extra

Link the drag of the attributes of the DIV element to the properties of the object in knockoutjs

I have here an object that will represent the div element function ElementVM { var self = this; self.Height = ko.observable(); self.Width = ko.observable(); self.Bottom = ko.observable(); } and bind this to <div data-bind="style:{ height: Height,

How to extract the last element of a string and use it to develop an array in a loop

I have a dataset like this: 10001;02/07/98;TRIO;PI;M^12/12/59^F^^SP^09/12/55 ;;;;;M1|F1|SP1;11;10;12;10;12;11;1.82;D16S539 ;;;;;M1|F1|SP1;8;8;8;8;10;8;3.45;D7S820 ;;;;;M1|F1|SP1;14;12;12;11;14;11;1.57;D13S317 ;;;;;M1|F1|SP1;12;12;13;12;13;8;3.27;D5S8

Keep the width of the div element with the background image

I have <div> element with background image from sprite. On left and right of that <div> I have <input> buttons (prev, next). The thing is that <div> with background doesn't seem to have any width, so it is squeezed between (and cov

The DIV element does not cover all its content

I have an issue with the div on my page. It's the only one I have, and it covers the middle of my page. With a few tweaks in CSS, I made it go all the way down. The problem though, is that the video(which is inside the div element), is sneaking out l

How can I place the div element in a canvas

i am trying to put the div element in to canvas. My div element has so many images.that div element i want to put it on canvas. How can i put the div element to canvas? here is my code: <canvas id="myCanvas" width="200" height="

Regular expression extracting the last element beginning with & amp; [?

The regular Expression should be applied to the following string: [Product].[Product Catalog].[Product].&[50]&[53]&[DE]&[1ST_HQ]&[50_54_36] As-Is: The following regular Expression is applied: (?:&\[).+(?:\]) That Returns: &[50]

to remove the div element from many div elements using jquery?

how to hide/remove a div from many div being generated using jquery on click ? below code for repeating boxed: var el = $("div"); var bottom = $(window).height(); while (el.offset().top + el.height() < bottom) { el = el.clone().insertAfter(el

The size of the DIV element does not grow automatically

The DIV elements height is not growing even after I set the height property as auto.please see the CSS snippet below #main_site { position: absolute; background-position: center; top: 295px; width: 950px; background-color: #FFF; height: auto; } I hav

Scroll the div element to HTML

When I press the button (+ Add card) the div element is created, like a card. At the same time I have scroll, covering the card. Like in the image below. I don't want this. How can I avoid this? Demo /*Style of a inbox list*/ #inboxList { width: 275p

How to parse an xml and extract the xslt element to javascript

I am sending an xml response from my servlet to my html page. I receive it via the xmlresponse object of the xmlhttprequest object. My xml document contains a xsl:stylesheet as an element I want to extract this element and execute that xslt code in m

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK