TheMysticalSock TheMysticalSock - 1 year ago 84
HTML Question

Do an action based on presence of ALT attribute with Javascript

I have some javascript which looks at the body and finds words and if one is present, it outputs a div. This is useful for many things, however...

What I need to do is also look at the body and all the ALT tags for the page as well.

I found this: Use javascript to hide element based on ALT TAG only?
Which seems to change the ALT attribute, however I want to perform an action.

Here's my JS so far.

var bodytext = $('body').text();
if(bodytext.toLowerCase().indexOf('one' || 'two')==-1)

var elem = $("<div>Text Here</div>");

Thank you.

P.S. I am a N00B/ relatively new at JS, I am doing this for a small project, so I am not sure where to start for this in terms of JS functions.

Answer Source

I'm not sure exactly what you would like to do here, you should add more detail. However if you choose to use jQuery, it provides tons of useful methods including the method .attr(), which lets you get the value of any attribute of any element.


var alt = $('#my-selector').attr('alt');
if (alt == 'whatYouWant') {
} else {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download