1

Our inventory web page was written in php by someone else. I am trying to put a jquery call into the webpage which will make a description appear whenever someone scans a barcode. Everything is working when I test on jsbin.com but I am having trouble getting it to run in the wild. The jquery src parts are in the footer before /body. It doesn't seem like they are wrapped by the ?php tags so I didn't put in echos.

</form>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">    </script>
    <script src="inventory.js"></script>
</body>

The problem may be in the javascript though.

var barcodes ={
    "000-091-M”:”Hardware - Mini Gender Changer Male to Male",
    "000-092-M”:”Hardware - Mini Gender Change Female to Female",
    "000-DVI-FF”:”Hardware - Dual Link DVI-I Female to Female Coupler Adapter",
};

$("textarea[id=details1]").on("input", function(){ //on value changed
    this.value; //text in textarea details
    $("textarea[id=description1]").text(barcodes[this.value]); //lookup barcode and return description into description1
});

The html part this affects would be

<td><textarea id="quantity1" name="quantity1" cols="2" rows="1"></textarea></td>
<td><textarea id="details1" name="details1" cols="25" rows="1"></textarea></td>
<td><textarea id="description1" name="description1" cols="35" rows="1"></textarea></td>
<td><textarea id="message1" name="message1" cols="35" rows="1"></textarea></td>

When they scan the barcode into details1 it should auto populate description1 with the description.

What have I done wrong?

8
  • 3
    You have two jquery library request... remove the first Commented Oct 7, 2013 at 18:42
  • generally speaking, <script> tags should go in the <head> section. Commented Oct 7, 2013 at 18:42
  • 1
    You seem to be using two different kinds of quotes like in "000-091-M” the second type seems to be phony. Try replacing them with ". Commented Oct 7, 2013 at 18:42
  • 2
    @MarcB Not necessarily. Commented Oct 7, 2013 at 18:44
  • In fact it's common practice in some places to put script tags at the end of the body. I don't remember why this is done though. Something about streamlining the loading maybe. Commented Oct 7, 2013 at 18:48

1 Answer 1

0

Without seeing the HTML can't be sure but it should look like

$("#details1").change(function(){ //on value changed
    //text in textarea details
    $("#description1").text(barcodes[$("#details1").val()]); //lookup barcode and return description into description1
});

The first argument of .on should be the type of event you're expecting. .change() looks for a change event. While there is an input event, this thread has a pretty good warning about why not to use it. Change is typically the best event to bind to

EDIT

Ok, based on your comments it sounds like you use PHP to populate the fields on page load. If you do that, JS won't trigger any events. So what we need is to trigger that JS function on page load (another solution would be to load via AJAX)

$(document).ready(function () {
    var val = $("#details1").val();
    if(val !== '') $("#description1").text(barcodes[val]);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, so based on your HTML that should work but it sounds like you have another process modifying the textarea. Is that a JS event also?
No, it's a php event. It grabs what is put into the form and emails it off.
The php code seems to end with ?> before <html>. Do I still need echos?
If PHP is editing it (in a non-AJAX way) then no JS events will be fired. I'll edit my response to show you how to trigger that
If your output is within a PHP block then it must be inside an echo statement. You don't have to have a ?> if your PHP block ends the page

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.