Selecting src from an input using ID in Jquery

Asked

Viewed 350 times

3

if(!$("#src-img[src='']")){

}else{

};

I would like to know what is wrong with this if selection, in which case I want, if a value is set in the src attribute, it to execute if, otherwise Else.

*That’s why I put the ' ! '

  • 1

    Can you explain what you want to do? Just like it gets confused because $(...) remove an object and always give true when converted to Boleane; and as Ids are unique that selector #src-img[src=''] doesn’t make much sense... do you want to use if (!$('#src-img').attr('src')){?

  • as I would to take the value of src and compare if it is empty or if it has something ?

  • 1

    In that case it is as I suggested in the comment above.

  • But then he doesn’t just compare whether the property exists or not? because I can define it, but leave it empty, and yet it would return as if it had something typed into it, no ?

  • 1

    With .attr() it fetches the value. If the value is an empty string it gives false.

  • 1

    ah yes, put the answer there, if it works here you already have the guaranteed answer :D

Show 1 more comment

3 answers

5

You could use the . attr like this:

if($('#img').attr('src') != ""){

}

This way it will check if the src attribute of your image element is not empty and if it is not if it will be true, otherwise you can put an Else.

  • 1

    I find this solution more elegant than mine.

  • 1

    It is possible to do only with if( $('#img').attr('src') ) also @Leoletto. When no specific verification is passed, it tests whether it is an empty string, 0, Undefined, null, spaces.

3

When jQuery finds no element, an object comes arraylike empty. This is not considered logically false by javascript, so the code you posted is equivalent to:

if (true) {
}

What you can do, is take a look at the property length jQuery return. If you have not obtained any element, the length will be zero. Then you can compare...

if ($("blablabla").length !== 0) {
}

Or use direct length, since zero is considered logically false.

if ($("blablabla").length) {
}

2


If I understand correctly, what you are looking for is to know if a given element with the id #src-img whether or not it has an attribute src assigned. For this you can use different selectors.

Example to clarify:

['#src-imgA', '#src-imgB', '#src-imgC'].forEach(function(id) {
    console.log(id);
    console.log(!!$(id).attr('src'));
    console.log(!!$(id + "[src]").length);
    console.log('-------');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="src-imgA" />
<img id="src-imgB" src="" />
<img id="src-imgC" src="/o/meu/url" />

The commented result:

#src-imgA
false     // o elemento existe, mas o atributo "src" está vazio/não existe
false     // o elemento existe mas como não tem atributo "src" o seletor falha
-------
#src-imgB
false     // o elemento existe mas o atributo "src" está vazio
true      // o elemento existe e tem um atributo "src" (ele não verifica se está vazio)
-------
#src-imgC
true
true
-------

Browser other questions tagged

You are not signed in. Login or sign up in order to post.