4

This is some simple JavaScript code I would like to implement, butit does not work.

I'm trying to get the file link inside the tag file but I think innerHTML does not work in this kind of situation, but I've given it a shot to try.

Could you guys help me with this? I know I'm missing something 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>
2
  • What is the [videoplayer] tag? What language are you using? Commented Dec 9, 2011 at 18:04
  • this is a wordpress plugin and I'm trying to extract the link automatically for every video. Commented Dec 9, 2011 at 18:10

2 Answers 2

4

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

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

alert( str );

Probably a good idea to also make sure the element was 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 );
Sign up to request clarification or add additional context in comments.

8 Comments

wow fast answer! this is my first time seeing something called getAttribute() let me try it Thanks!
@iMohammad: You're welcome. You could also try .file instead of .getAttribute('file'), but I don't know if that attribute shows up as a property.
I just tried it, still it does not extract and print the link. Kindly Could you try the code for me?
@iMohammad: Did you use an alert() instead of document.write? Also did you try my updated answer? Place another alert() inside the if() statement to make sure an element was found. There really isn't much information to work from in your question.
@AdamRackis: Good idea. I should really be more productive right now. :)
|
2

You use the getAttribute function:

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

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

<foo>
  Hello!
</foo>

But that is not the case for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.