dragonfly
dragonfly

Reputation: 3227

Passing function variable as id

I want to attach "p-rating" to dynamic id to $("<div id='slider'></div>") Expected result would be $("<div id='slider p-rating'></div>") I wanted to get the value strictly from select parameter as I'm planning to that be dyanamic. SELECT holds that value: p-rating but how to pass that value? I wanted space between ids

function initiateSlider (select) {
        var slider = $("<div id='slider'></div>").insertAfter( select ).slider({
        ...
        })
    }
$(function() {
        var select = $("#p-rating"); //plan-preference slider
        initiateSlider(select);    
      });

Upvotes: 0

Views: 117

Answers (2)

rogMaHall
rogMaHall

Reputation: 691

This sounds like a good time to use jQuery's .data(). So adding the p-rating to the div would go:

$('#slider').data('p-rating', select.val());

Upvotes: 2

Jordan
Jordan

Reputation: 2039

Could you call the JQuery attr function while referencing it's ID attr within itself?

var sliderOriginalId = $("#slider").attr('id');

var sliderNewId = $("#slider").attr('id', sliderOriginalId + " *p-rating*");

EDIT: This ID would be invalid with the space.

The thing is a single element (HTML tag) can't have more than one ID. It can have more than 1 class though. You might want to consider adding classes instead of id. (Depending on what you are doing)

var slider = $("#slider").addClass("p-rating").addClass('slider');

Upvotes: 0

Related Questions