Search code examples
javascriptjqueryhtmlajaxkohana

JQuery Data attributes getting stripped out in Kohana ajax


I'm working on an app where they have gone with jquery data to pass variables to html

it works in one aspect of the site when the data attributes are attached to a tr tag. This code works

<tr class="js-instructions-row documents__table-row 
<?=$ix.'row';?><?php //$ix==0 ? 'documents__table-row--active' : '' ?>"
data-product-title="<?= $sheet->name ?>" 
data-instructions-image="<?= $serverpath.$thisImage ?>"
data-instructions-file="<?= $serverpath.'Instructions/'.$sheet->file ?>"
>

when I try to put those attributes on a select tag or option tag in another view, it doesn't come through. This code doesn't work.

<?php
foreach($instructions as $ix => $sheet) {
$thisImage = ($sheet->image?$sheet->image:'Image_holder_thumb.png');
?>
<option test="" data-product-title="<?= $sheet->name ?>" data-instructions-image="<?= Kohana::$config->load('aws.s3-baseurl-www-customercare').$thisImage ?>" data-instructions-file="<?= Kohana::$config->load('aws.s3-baseurl-www-customercare').'Instructions/'.$sheet->file ?>" value="<?=$sheet->id?>"><?=$sheet->name?></option>
<?php
}
?>

and offending javascript:

$('.js-product-selector').on('change',function(e){

var selected = $(this).find('option:selected');  
console.log(selected.attr('value'))
console.log(selected.data('product-title'));
$(".documents__product-title").text(selected.data('product-title'));
$(".documents__preview img").attr('src',selected.data('instructions-image'));
$(".documents__download").attr('href',selected.data('instructions-file'));

});

value attribute comes through just fine in the log but data-product-title does not here is how my view is called in the Controller.

$this->response->body(View::factory($this->folder."/instruction-sheets")->set('brands',ORM::factory('Brand')->with('Customercare_Instruction')->find_all())->set('postbrand',$brand));

The view that works is nested inside of a view that is called like this:

$this->page=View::factory($this->folder.'/index');
$this->page->breadcrumb = 'Instruction Sheets';
$this->page->content = View::factory($this->folder."/instruction-sheets")->set('brands',ORM::factory('Brand')->with('Customercare_Instruction')->find_all());

and the sub-view is called like this

<?= View::factory('customer-care/instruction-sheets-filtered')->set('instructions',$instructions)->render() ?>

I appreciate your input.


Solution

  • You wouldn't by any chance be styling your select element with something like Selectize or Select2, would you? That's probably what's stripping the data attributes from your options.