Ankur
Ankur

Reputation: 51128

How to mimic an HTML element and add attributes to it in Javascript/jQuery

I have a method called handle_value_tag_click(value) which takes an HTML element (value) as a parameter.

I want to call this function from another function, but this time I don't have a 'ready made' HTML element ... in the initial situation I pass a this element and the function unwraps the parameters appropriately.

My guess is I have to construct the element by using a JS object. This is my attempt:

    var value = new Object();
        value.attr("valueType","NumericQueryValue");
        value.attr("lower",lowerBound);
        value.attr("upper",upperBound);
    handle_value_tag_click(value);

However I get the error value.attr is not a function, how can I solve this error, or get the appropriate behavour (passing parameters to the handle_value_tag_click() function) in some other way.

Upvotes: 0

Views: 157

Answers (2)

Mo Valipour
Mo Valipour

Reputation: 13496

what if you say this?:

var value = $("<div />");

Upvotes: 5

KARASZI Istv&#225;n
KARASZI Istv&#225;n

Reputation: 31477

Why don't you create an element like this:

var value = $(document.createElement("div"));

It won't be in the DOM but every jQuery method will work on it.

Upvotes: 0

Related Questions