Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add image if text contains certain word?

Status
Not open for further replies.

divtag32

Programmer
Sep 22, 2006
2
GB
Ok I'm a total beginner, so please be gentle!

I basically am wondering how hard it would be to use javascript to automatically incldue an image depending on whether a certain word was in a text box?

I've got two <div> boxes, one with a pile of text and the other with a pile of images - at the moment I have to read the whole text of every article to ensure that if the article refers to 'Nike' there is a nike image inserted in the second <div> (the second <div> is simply a float:right box full of logos)

Is this possible with javascript?

Many thanks!
 
It is, although you would probably be better off doing this server-side and delivering different content, rather than doing it client-side, if possible.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi there,

well yes I'd love to - indeed the whol ething would be far easier just using a CMS really to deal with all these articles!

But unfortunately it's a nogo - it has to be client-side due to hardware restrictions.

Do you know how this can be achieved in javascript or know of any tutorials/examples?
 
Sure it can. The eaxct code depends on your page, which we know nothing about... but the general principle would be to search for the text:

Code:
if (el.innerHTML.indexOf('Nike') != -1) {
   imgEl.src = 'nike.jpg';
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top