2
<script type="text/javascript">
    function GetSrc(elem)
    {
        alert ( elem.src );
    }

</script>

<img src="images/yourimage.extn" id="img1" onclick="GetSrc(this);" />

i want to convert elem.src to string, String(elem.src); not working

1
  • elem.src is already a string, you can assign it to another string object or if you still have doubts, you can use .toString() method Commented Mar 3, 2010 at 13:58

3 Answers 3

3

It's already a string. You could also try:

var srcString = new String(elem.src);

but it is unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

0

You can already use the source as a string.

alert( document.getElementById("img1").src ); // images/yourimage.extn

You can set it too:

function GetSrc(elem) {
  elem.src = "http://kol.coldfront.net/thekolwiki/images/c/ca/String.gif";
}​

Comments

0

It is a string itself. No need to again convert to string.

alert ( typeof(elem.src) );

will give you a string

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.