0

I'm having a little issue, for styling purposes I'm using a Definition list instead of a combobox, inside my dd tag, there's an ul, inside every li, there's a span, with a value attribute. I'm trying to put that value on a text field, so I can use it in a post action. This code is inside the click function of my li:

var temp = $(this).find("span").attr("value");
$("#ch").val(temp);

I believe my selection is right, because when I log temp's value, it is the one I want, but when I log the ch field's value, I get undefined.

What am I missing? Thanks in advance.

José.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
jsdgdo
  • 65
  • 1
  • 2
  • 6

2 Answers2

2
$("#ch").val($(this).find("span").text());

However if you're sure temp has the correct value (even when using the wrong method), then you're probably not hitting your target, are you sure the element has the ID ch ?

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Who in the right mind would give a `value` attribute to a `span` element (per your last update)? `span` elements don't have a `value` attribute unless you define it in the markup yourself. – Fabrício Matté Jul 24 '12 at 00:48
  • @FabrícioMatté - who knows, but the question does state that the `temp` variable supposedly contains the right value ? – adeneo Jul 24 '12 at 00:49
  • Oh my bad, I missed the part that he stated to be getting the right value in `temp`. My point still stands though, could use `data-` attributes or another hundred of better ways around it. Here's some [reference](http://stackoverflow.com/q/992115/1331430) for OP if he is creating custom attributes. – Fabrício Matté Jul 24 '12 at 00:51
  • @FabrícioMatté - I agree with you, and I had to read the question four times to actually see that it is in fact stated that the `temp` value is right, so that leaves the second line, and there ain't much that go wrong there, other than the selector ? – adeneo Jul 24 '12 at 01:00
  • Yup, either that or maybe, `#ch` is not an `input` element and thus doesn't have a `value` property to be set with `.val()`? If `#ch` is a `span` for example, `$('#ch').html(temp)` or `$('#ch').text(temp)` should be used. – Fabrício Matté Jul 24 '12 at 01:02
  • Oh nvm my comment above, just made a [fiddle](http://jsfiddle.net/ult_combo/n5nBf/) and if the element doesn't have a `value` property, jQuery will create it. Seems like the selector was indeed faulty. +1 – Fabrício Matté Jul 24 '12 at 01:07
  • I forgot to put my id attribute on my input, just put the name attribute, there was my mistake. Thanks so much! – jsdgdo Jul 24 '12 at 01:10
1

Just to know, span elements don't have a value attribute unless you define it in the markup yourself.


Possible ways to achieve what you want:

Use this:

$('#ch').val($(this).find('span').html());

Or:

$('#ch').val($(this).find('span').text());

Inside this:

$(selector).on('click', function() {
   $('#ch').val($(this).find('span').text());
});

Possible solutions to undefined problem:

  • Make sure you are using the correct id for the element you want to set the value.
  • Maybe your element #ch is in DOM, so you will need to use .on() as the above example instead of just $(selector).click();
Oscar Jara
  • 14,129
  • 10
  • 62
  • 94