JavaScript: extracts the value of the div element
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
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
Using max-height on the DIV element
Get the class name in the div element by xpath
Hide the div element when the screen size is less than a specific size
Link the drag of the attributes of the DIV element to the properties of the object in knockoutjs
How to extract the last element of a string and use it to develop an array in a loop
Keep the width of the div element with the background image
The DIV element does not cover all its content
How can I place the div element in a canvas
Regular expression extracting the last element beginning with & amp; [?
to remove the div element from many div elements using jquery?
The size of the DIV element does not grow automatically
Scroll the div element to HTML
How to parse an xml and extract the xslt element to javascript
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK