loumi
loumi

Reputation: 93

Retrieve element value

I would like to retrieve the value of the following dl element to send it to a php file with an ajax request

<dl id="item" value="9">9</dl>

I tried all of the following code but none worked :/

var item = $('#item').attr('value');
var item = $('#item').val();
var item = $('#item').text();
var item = $('#item').html();

I don't know if it's relevant but here is the ajax request

$.ajax({
    url: 'myPhpFile.php',
    type: 'get',
    data: { 'item': item },
    success: function(data) {
        $('#myDiv').html(data);
    }
});

Also, on firebug there is no error, however the url doesn't have any parameter on it. What am I doing wrong?

Upvotes: 0

Views: 85

Answers (3)

Andrew Miner
Andrew Miner

Reputation: 6125

Your code looks correct in the given example, so I would guess there's something else going on. A few possibilities:

  • Your selector (i.e., #item) is incorrect.
  • Your code is being run before that element is added to the DOM
  • The item variable is in a different scope from the AJAX request (i.e., it's value is null in the AJAX request's scope)
  • The item variable is being overwritten before you make the request.

The easiest way to proceed is probably to step through with a debugger so you can watch both the DOM and the variables to see exactly what is happening at each step.

Upvotes: 2

user1994667
user1994667

Reputation:

Your Dl tag probably has DT tags inside. If you take the DL value, it won't return the values of its child elements If I'm correct that you want to get child values, try this way

$('#dl dt:').val()
$('#dl dd:').val()

Upvotes: 0

James Curran
James Curran

Reputation: 103485

var item = $('#item').attr('value');

should be the correct choice. I don't know why it's not working for you.

It's demonstrated here.

Upvotes: 0

Related Questions