2

I'm using Zend Framework 2. I would like to know how to get data defined in html in my javascript code.

html

<tr class="MyClass" data-MyData="<?php echo json_encode($array);?>">

javascript

$(document).on('click','.MyClass', function () {

        var temp =document.getElementsByClassName("data-MyData");

        $.ajax({
            url: path_server + "pathDefinedInMyConfig",
            type: 'post',
            encode: true,
            dataType: 'json',
            data: {
                'temp ': temp 
            },
            success: function (data) {
                //some code
            },
            error: function () {
                alert("ERROR");
            }
        });
});

The problem is I don't have access to row in my Controller method. And i want to have access to My $array defined in html in my Controller.

2
  • do you know which element is the tr with class MyClass in your page? I mean: 1st? 3rd? 4th? Commented Feb 24, 2017 at 8:25
  • you may change var temp =document.getElementsByClassName("data-MyData"); to var tmp = this.getAttribute('data-MyData'); Commented Feb 24, 2017 at 8:29

5 Answers 5

1

Problem is that you are trying to find a class by the name data-MyData, but the object you "want" to look for is "MyClass"

Try something like var temp =document.getElementsByClassName("MyClass").attr("data-MyData");

Even better is that since you click on the object with MyClass you can use $(this).attr('data-MyData');

then result will look like: var temp = $(this).attr('data-MyData');

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

Comments

1

Simply replace temp var assignment line:

var temp =document.getElementsByClassName("data-MyData");

with this one:

var temp = this.getAttribute("data-MyData");

Comments

0

Since you use jQuery use the following:

$('.MyClass').data('MyData')

or

 $('.MyClass').attr('data-MyData')

Comments

0

use like this-

$(document).on('click','.MyClass', function () {

        var temp =$(this).attr('data-MyData');

        $.ajax({
            url: path_server + "pathDefinedInMyConfig",
            type: 'post',
            encode: true,
            dataType: 'json',
            data: {
                'temp ': temp 
            },
            success: function (data) {
                //some code
            },
            error: function () {
                alert("ERROR");
            }
        });});

Comments

0

You have a wrong selector. document.getElementsByClassNames() returns the collections so you have to loop through to get the target element:

var temp =document.getElementsByClassName('MyClass');
[].forEach.call(temp, function(el){
   console.log(el.dataset['MyData']);
});

or as you are using jQuery then you can use .data():

var temp =$(this).data("MyData");

and with javascript:

var temp =this.dataset["MyData"];
// var temp =this.dataset.MyData; // <---this way too.

Comments

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.