0

I need to retrieve attribute value of <div> tag using jquery

This is my html code where $_GET['availabel_table'] and $_GET['number-of-person'] are coming from url

<div id=main_menu_data class="grid_21 alpha  prefix_1" availabel-table="<?php echo $_GET['availabel_table'] ?>" number-of-person="<?php echo $_GET['number_of_person'] ?>" >

Jquery code

var availabel_table = $("#main_menu_data").attr("availabel-table");
var number_of_person = $("#main_menu_data").attr("number-of-person");

but value is not coming in these variables. What i am doing wrong.

4
  • 4
    It's not valid HTML, should still work, but you should really be using data attributes. Commented Mar 1, 2013 at 17:49
  • 1
    Are you sure your values are there in the source? Commented Mar 1, 2013 at 17:49
  • 3
    It is spelled "available" FYI, would drive me nuts if I inherited that code :) Commented Mar 1, 2013 at 17:49
  • Try data-availabel-table Commented Mar 1, 2013 at 17:52

4 Answers 4

2

This:

<div id=main_menu_data

should be this:

<div id="main_menu_data"
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, there's lot of incorrect HTML in there, must correct it to ensure that it's not the actual problem.
1

Missing quotes on the DIV's id?

<div id="main_menu_data" class="grid_21 alpha  prefix_1" availabel-table="<?php echo  $_GET['availabel_table'] ?>" number-of-person="<?php echo $_GET['number_of_person'] ?>" >

Comments

0

You should set your data values with with valid data attributes, your code would be something like:

  <div id="main_menu_data" class="grid_21 alpha  prefix_1" data-available-table="<?php echo     $_GET['availabel_table'] ?>" data-number-of-people="<?php echo $_GET['number_of_person'] ?>" >

then to access this data, you can use the jQuery data function:

  var available_table = $("#main_menu_data").data("available-table");
  var number_of_people = $("#main_menu_data").data("number-of-people");

Comments

-1
var numberPerson= $('#number_of_person').val();
  var availabelTable= $('#availabel_table').val();

of course that you should be declare thats variables in html

1 Comment

val() will not return data attributes on a div element.

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.