Question

I know I'm making some simple mistake, I just can't seem to see it.

<li class="ui-state-default" id="1">Text I want to pull</li>

$('#control1').prop('value', "test" );           //this works
$('#control2').prop('value', ('#1').text() );    //this does not work

//OUTPUT:
<input type="hidden" name="control1" id="control1" value="test">
<input type="hidden" name="control1" id="control1" value="">

I am using $(document).ready(function() { //code });

I am populating the ID field of the list item with jQuery (there are 8 list items):

//this code works fine the DOM updates and I have numbered list items id=1...id=4...id=8
var controlnumber = 1;
$( "li" ).each(function() {
  $(this).prop('id', controlnumber);
  controlnumber++;
  });
Was it helpful?

Solution

First, it's $('#1') not ('#1')

second, For HTML 4

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number     of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

So, change the $('#1') to $('#A1') or any other names you like begin with a letter.

OTHER TIPS

Yes. You are missing $. You should do: $('#1').text();

$('#control2').prop('value', $('#1').text() )
                             ^------------------ add $ here.

Also why don't you simply do this?

$('#control2').val($('#1').text());

Also it is worth noting that only numberical ids are a bad idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top