0

this is a very simple if statement how ever i have tried nummours ways to type the code. what i am looking to do is making sure the .src of "captch_style" to be == source_one

html

<img id="captch_style" src="img/captcha/captcha_0.jpg" 
 width="200" height="60" alt=/>

seperate js file

<script type=text/javascript>


var source_one="img/captcha/captcha_0.jpg";

if (document.getElementById("captch_style").src === source_one){

        inputValue.style.borderColor="#00ff00";
}   

else {
inputValue.style.borderColor="#ff0000";

}

<script>
1
  • 4
    You shouldn't have the script tag in your js file. Commented Jan 26, 2014 at 11:03

1 Answer 1

2

The src property usually contains an expanded URL, not a relative one. So you're comparing (for instance), http://example.com/img/captcha/captcha_0.jpg with img/captcha/captcha_0.jpg and so it doesn't match.

You can check if it just ends with that:

if (document.getElementById("captch_style").src.substr(-source_one.length) === source_one) {
    // It does
}
else {
    // It doesn't
}

The substr function returns a substring of the string you call it on. If you call it with a negative index, it starts from the end of the string and counts back, so "abcdef".substr(-2)gives you"ef"`.

Alternately, you could just get the src attribute rather than the property (.getAttribute("src")), which should contain whatever you have literally in the HTML for the image, but some browsers (just old ones?) may expand that improperly. (I don't recall if it happens with the src attribute, it certainly used to with href on a elements.)


Separately, as Shomz points out in a comment, you must not have a script tag in your JavaScript file; I'd expect that to give you a syntax error, though perhaps some browser JavaScript engines may tolerate it. A JavaScript file just contains JavaScript code. script tags are for putting blocks of JavaScript code within an HTML file.

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

2 Comments

would this same method work for color? if i was to check for the text color? var spam=document.getElementById("margin06"); var color="#ff0000"; document.getElementById("margin06").color.substr(-5) === color.substr(-5)
@user3237322: It would, if: 1. Elements had a color property (perhaps you meant .style.color?), 2. You really only wanted to check five (not six) of the characters of the color, and 3. If that property were reliably in hex notation, which it is not. In fact, different engines will use different notations for that property's value, including hex (the notation you used), rgb, rgba, a color name (such as red), ... In theory, they could even use hsl, but I haven't seen one do so (yet).

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.