Selecting image using $('img[src$="image source end string in variable"]') getting undefined value
<p>I have this code wherein there's an image in id <code>#imgfullview</code> and images on class <code>imglist</code>.</p>

<p>I want to get the image src of <code>#imgfullview</code> and get the matching image on class imglist base on src attribute.</p>

<p>I used this selector <a href="https://api.jquery.com/attribute-ends-with-selector" rel="nofollow noreferrer">https://api.jquery.com/attribute-ends-with-selector</a></p>

<p>This is working if i just used selector using string</p>

<pre><code>var x = $(".imglist[src$='b-640x624.jpg']");
var imglistmatched = x.attr("src");
alert(imglistmatched );
</code></pre>

<p>But if I used a selector changing the string to variable, I'm getting <code>undefined value</code> not sure why though I have exact string as <code>'b-640x624.jpg'</code> on variable <code>splitimgurl</code> from string split function based on full patch src or through assigning the exact string if I check it on console.log or alert the variable</p>

<pre><code>var currentimg =$("#imgfullview").attr("src");
var splitimgpath = currentimg.split('_');
var splitimgurl = splitimgpath[2]; //or this splitimgurl = "b-640x624.jpg";
var x = $(".imglist[src$=splitimgurl]");
var imglistmatched = x.attr("src");
alert (imglistmatched);
</code></pre>

<p>I tried this</p>

<pre><code>var x = $(".imglist[src$="splitimgurl"]");
</code></pre>

<p>and this</p>

<pre><code>var x = $(".imglist[src$="+splitimgurl+"]");
</code></pre>

<p>and this</p>

<pre><code>var x = $(".imglist[src$=""+splitimgurl]");
</code></pre>

<p>it does not work</p>

<p>Please see this on fiddle <a href="http://jsfiddle.net/w7n3sh2v/" rel="nofollow noreferrer">http://jsfiddle.net/w7n3sh2v/</a></p>

<p>I found the related question below but it does not help</p>

<p><a href="https://stackoverflow.com/questions/3956157/jquery-imgsrc-the-image-souce-attrsrc-new-src-does-not-work">jQuery $(&quot;img[src=the_image_souce]&quot;).attr(&#39;src&#39;,&#39;new_src&#39;); does not work</a></p>

<p>May I know if there's a correct way to put variable on Attribute Equals Selector?</p>