JavaScript substring from element

This is simple JavaScript code that I would like to implement but does not work.

I am trying to get a link to a file inside the file tag, but I think that innerHTML does not work in a similar situation, but I gave it a chance to try.

Could you guys help me with this? I know something is missing here.

 [videoplayer id="one" file="/dir1/dir2/hi.mp4" width="333" height="333" /] <script type="text/javascript"> var str = document.getElementById('one').innerHTML; document.write(str.substr(0,str.length-1)); </script> 
+4
source share
2 answers

Use getAttribute() to get the value of the file attribute.

 var str = document.getElementById('one').getAttribute('file'); alert( str ); 

It might be a good idea to also make sure the item is found:

 var el = document.getElementById('one'); if( el ) { alert( 'Element was found!' ); var str = el.getAttribute('file'); } else { alert( 'NO element was found' ); } alert( str ); 
+4
source

You use the getAttribute function:

 var str = document.getElementById('one').getAttribute('file'); 

innerHTML only works if there is HTML code inside the tags:

 <foo> Hello! </foo> 

But that does not apply to you.

+2
source

Source: https://habr.com/ru/post/1385579/


All Articles