0

I am trying to return the description value of the corresponding author name and book title(that are typed in the text boxes). The problem is that the first description displays in the text area no matter what.

<h1>Bookland</h1>
<div id="bookinfo">
    Author name: 
    <input type="text" id="authorname" name="authorname"></input><br />
    Book Title:
    <input type="text" id="booktitle" name="booktitle"></input><br />
    <input type="button" value="Find book" id="find"></input>
    <input type="button" value="Clear Info" id="clear"></input><br />
    <textarea rows="15" cols="30" id="destin"></textarea>
</div>

JavaScript:

var bookarray = [{Author: "Thomas Mann", Title: "Death in Venice", Description: "One of the most famous literary works of the twentieth century, this novella embodies" + "themes that preoccupied Thomas Mann in much of his work:" + "the duality of art and life, the presence of death and disintegration in the midst of existence," + "the connection between love and suffering and the conflict between the artist and his inner self." },
                 {Author: "James Joyce", Title: "A portrait of the artist as a young man", Description: "This work displays an unusually perceptive view of British society in the early 20th century." + "It is a social comedy set in Florence, Italy, and Surrey, England." + "Its heroine, Lucy Honeychurch, struggling against straitlaced Victorian attitudes of arrogance, narroe mindedness and sobbery, falls in love - while on holiday in Italy - with the socially unsuitable George Emerson." },
                 {Author: "E. M. Forster", Title: "A room with a view", Description: "This book is a fictional re-creation of the Irish writer'sown life and early environment." + "The experiences of the novel's young hero,unfold in astonishingly vivid scenes that seem freshly recalled from life" + "and provide a powerful portrait of the coming of age of a young man ofunusual intelligence, sensitivity and character. " },
                 {Author: "Isabel Allende", Title: "The house of spirits", Description: "Allende describes the life of three generations of a prominent family in Chile and skillfully combines with this all the main historical events of the time, up until Pinochet's dictatorship." },
                 {Author: "Isabel Allende", Title: "Of love and shadows", Description: "The whole world of Irene Beltran, a young reporter in Chile at the time of the dictatorship, is destroyed when" + "she discovers a series of killings carried out by government soldiers." + "With the help of a photographer, Francisco Leal, and risking her life, she tries to come up with evidence against the dictatorship." }]


function searchbook(){
    for(i=0; i &lt; bookarray.length; i++){
        if ((document.getElementById("authorname").value &amp; document.getElementById("booktitle").value ) == (bookarray[i].Author &amp; bookarray[i].Title)){
            document.getElementById("destin").value =bookarray[i].Description
            return bookarray[i].Description
        } 
        else {
            return "Not Found!"
        }
    }
}
document.getElementById("find").addEventListener("click", searchbook, false)
3
  • Your operators (<, &, etc) are getting escaped. It's making the code tough to follow. Commented Jan 10, 2011 at 1:47
  • FYI: input elements are self closing: <input .... />. And next time you don't need to add all your data. Three dots ... are enough for Description ;) Commented Jan 10, 2011 at 1:52
  • I had to escape them because it wouldn't run in Mozilla. I'm using XHTML Strict for the record. Commented Jan 10, 2011 at 2:54

2 Answers 2

1

Your code got html escaped for some reason, but I think the problem is in your if. Regardless, this should give you your answer and be slightly faster since it doesn't try to look up the elements in the dom inside a loop

function searchbook(){
  var author = document.getElementById('authorname').value;
  var title = document.getElementById('booktitle').value;
  for (var i=0, book; book = bookarray[i]; i++) {
    if (book.Title == title && book.Author == author) {
      return book.Description;
    }
  }
  return "Not Found"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, I ended up coming up with something similar before reading this. Would you happen to know why my else statement doesn't work?
Hard to tell but it looks like you were doing a bitwise and of the auhor and the title on the form and comparing to a bitwise and of the author and title in the object. Most of the time both sides would end up 0 since 'stringa' & 'stringb' == 0 and therefor your first if would always be true.
0

This code is a bit more flexible and will populate your textarea for you.

function searchbook(){
    var author = document.getElementById("authorname").value;
    var book = document.getElementById("booktitle").value;
    for(i=0; i < bookarray.length; i++){
        if(bookarray[i].Author.match(author) || bookarray[i].Title.match(book))
        {
             document.getElementById("destin").value = bookarray[i].Description;
             break;
        }
    }
}

I'm not sure why you're using a textarea if there's no user input to be had on that particular element.

2 Comments

Sorry I dont get what you mean?
Why use a textarea to populate data if you don't expect anyone to use it to input anything? You're better off using a <div> or a <p> tag.

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.