0

I tried to get value of image src with click event by using this:

<img onclick='swap(this); return false;' style='width:auto;max-width:65px;max-height:55px' src="/member/sthumb/$image_path_array[$i]">

So when i click it, i get this value /member/sthumb/$image_path_array[$i]

Here is my javascript code:

<script type="text/javascript">
   function swap(image) {
      ...some code to cut the sthumb/ part from the string
      document.getElementById("main").src = image.src;
   }
</script> 

In this javascript code, i would like to cut this specific part sthumb/ out so the image.src will now be: /member/$image_path_array[$i]

2 Answers 2

1

Use String.replace()

function swap(image) {
    //...some code to cut the sthumb/ part from the string
    document.getElementById("main").src = image.src.replace('/sthumb', '');
}
Sign up to request clarification or add additional context in comments.

Comments

0
You can use simple replace method of String or it can be done using substring also
Solution1(Simple and Best):
function swap(image) {
    document.getElementById("main").src = image.src.replace('/sthumb', '');
}

Solution2:
function swap(image) {
var str=image.src;
var StringToCut='/sthumb';
document.getElementById("main").src = str.substring(0,str.indexOf(StringToCut))+str.substring(str.indexOf(StringToCut)+StringToCut.length);
}

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.