1
$("#button").click(function () {
    //debugger;
    if ($("img[id=image][src:contains(Grass)]"))
        $("img").attr({ src: "/Content/images/Spring.jpg", alt: "Spring" });
    else
        $("img").attr({ src: "/Content/images/Grass.jpg", alt: "Grass" });
});

<a href="/Content/images/Grass.jpg"><img src="/Content/images/Grass.jpg" alt="image"    id="image"/></a>
<input type="button" id="button" name="button"/>

I have simple form with image and button, i want on each button click image getting changed. The logic is very simple. By default image src = /Content/images/Grass.jpg When i click button first time image getting changed, but when i am click second time it is does not changed back. I check in debugger and find out that condition $("img[id=image][src:contains(Grass)]") always true. But after first button click shouldn't it became false then?

if i declare

    var img1 = $("img#image[src:contains(Grass)]");
    var img2 = $("img#image[src:contains(Grass2)]");
    var img3 = $("img#image[src:contains(blabla)]");

each img1.length = 1 img2.length = 1 img3.length = 1 Why?

but

var img1 = $("img#image[src*='Grass']");
var img2 = $("img#image[src*='Grass2']");
var img3 = $("img#image[src*='blabla']");

each img1.length = 1 img2.length = 0 img3.length = 0 it is what was expected.

Does it means that src:contains(text) and src*=text is so different?

5 Answers 5

3
$("#button").click(function () {
  var alt = ($('img#image[alt="Grass"]').length) ? 'Spring' : 'Grass';
  $('img#image').attr({ src: "/Content/images/"+alt+".jpg", alt:alt });
});

<a href="/Content/images/Grass.jpg">
  <img src="/Content/images/Grass.jpg" alt="Grass" id="image"/>
</a>
<input type="button" id="button" name="button"/>
Sign up to request clarification or add additional context in comments.

3 Comments

Should be "img#image", not image#img, why in that case is better to check alt?
but if i am wouldn't set alt. If alt is constant? I need to check src anyway.
there is no need to add the img before the #image selector. #image is highly optimized because its an id, and you should never duplicate id's so there will never be a #image that isn't a img
2

The line if ($("img[id=image][src:contains(Grass)]")) will always be true. The $() function returns a jQuery object, and an object always evaluate to true. You need to check that the length of the result is not 0.

if ($("img[id=image][src:contains(Grass)]").length > 0)

5 Comments

Incidentally, this code isn't a very good way to do things, and I would advise against using it in a production site. But if you're just experimenting with how jQuery works, then this answer will give you what you need.
Not working, i watch in debug, in both cases($("img[id=image][src:contains(Grass)]").length and $("img[id=image][src:contains(Spring)]").length ) at the same time i am getting length equal to 1
@Jason LeBrun I am just experimenting, but what is the better way of doing this?
It depends on your ultimate goal, but when I want to toggle between two images like this, I generally include them both in the source, with a unique id attribute set. Set visibility: none CSS property on the one I don't want shown. Then, to toggle between them, just change the visibility property for each one when the button is pressed. It's much more efficient to look something up by DOM ID then by attribute values.
[src:contains(Grass)] is an invalid selector. :contains searches the content of the element not of attributes. api.jquery.com/contains-selector
2

Try this:

var img = $("#image"),
    a = img.parent('a');
$("#button").click(function () {
    var str = (img.attr('src').search('Grass') != -1) ? "Spring" : "Grass",
        src =  "/Content/images/"+str+".jpg";
    img.attr({ src: src, alt: str });
    a.attr("href", src);
});

First we are caching the #image jQuery object, no need to select it each time the button is pressed. Then we do a normal javascript search on the src string. If it has grass in it then we change it to spring. Because we cached the #image jQuery object we don't have to reselect it to change the src and alt attributes. Also if you are changing the img src I am guessing you probably want to update the link around it.

2 Comments

Does it meant that in that case selector $("img[id=image][src:contains(Grass)]") working not as expected?
@Reg yes :contains is a special selector that doesn't work on attributes. api.jquery.com/contains-selector
0

One simple fix is to replace this:

$("img[id=image][src:contains(Grass)]")

...with this:

$("#image[src*=Grass]").length

Related selectors:

a^=b    a starts with b
a$=b    a ends with b
a!=b    a does not equal b
a*=b    a contains b

1 Comment

I am already fix this using [attribute*=value] instead of [attribute:contains(value)] but still researching why they are returning different results. [attribute*=value] vs [attribute:contains(value)]
0
          $("#button").click(function () {                
            var value = ($("img#image[src*='Grass']").length) ? 'Spring' : 'Grass';

            $("img").attr({ src: "/Content/images/" + value + ".jpg", alt: value });
          });

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.