1

I am having real problem with the following script where I am trying to compare the user selected input data (#texto div) to an AJAX called XML file and return an element (price) to a different div (#divo). It seems impossible to compare the text1 variable to the different XML attributes ? When I enter the name of the attribute instead of the text1 variable it does work. I hope I am clear enough.

Thank you for your help!

First the XML file:

<priceitem>
<name itemname="Ham Brie"><price>9.00</price></name>
<name itemname="croque monsieur"><price>8.00</price></name>
</priceitem>

Now the jQuery script:

$(function() {
$("#texto").select(function() {
  var text1 = $("#texto").val();

$.ajax({
  type: "GET",
  url: "price.xml",
  dataType: "xml",
  success: function(xml) {
  parsexml(xml);
    }
  });


function parsexml(xml) {
  $(xml).find('priceitem').each(function() {
  var att = $(this).find('name').attr('itemname');
  var price = $(this).find('price').text();
  if (att == text1) {
    $('#divo').append(price);
  }
  else {
    alert('problem!');
  }
 });
    }
});
});

2 Answers 2

1

UPDATE:

I am having real problem with the following script where I am trying to compare the user selected input data (#texto div) to an AJAX called XML file and return an element (price) to a different div (#divo).

Thats the problem right there...

  1. You can only call val() on form field controls - things that have a value attribute.
  2. You are not going to get the desired value even if you were able to call val on a DIV because what you want is the value of the selection not the entire DIV. To get that you need to get that data from window.getSelection() in modern and standards browsers, or document.selection.createRange() in IE < 9.
  3. Lastly jQuery.select only works on input and textarea elements as well, so the event will never even be fired for a DIV.

    From the docs:

    The select event is sent to an element when the user makes a text selection inside it. This event is limited to fields and boxes.

    You will need to use a series of other events in order to do this like mousedown and mouseup but its going to be a bit more complex. Or, there may be a plugin you can use but I don't have one i can recommend.


priceitem is the root node so you cannot find it. You need to adjust your code like:

function parsexml(xml) {
  $(xml).find('name').each(function() {
  var att = $(this).attr('itemname');
  var price = $(this).find('price').text();
  if (att == text1) {
    $('#divo').append(price);
  }
  else {
    alert('problem!');
  }
 });
    }

Example: http://jsfiddle.net/2gt1f44f/

Sign up to request clarification or add additional context in comments.

2 Comments

You are right on that part. However the script does not insert the variable price into #divo. and when I try to alert the var att I get an undefined????
What type of element is #texto? I think maybe you are confused on what the jQuery.select() function does and the event it binds to... You should post the relevant HTML you are working with.
0

Thank god for console.log! I had a stupid error in my php script that generated the datalist (one extra space so text1 and att where never being equal.....Now it is working. I cannot believe I spend hours on such a stupid mistake....Lesson: always use console.log. Thank you everybody!

3 Comments

So how did you get the event to fire on a DIV?
Sorry, it has been a long day and I made a mistake. I meant id for texto (referring to an input) and div for the answer.
Ok that makes more sense... Thanks for the follow up :-)

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.