manju
manju

Reputation:

how to assign variable value to jquery?

var s="attr"
var i=$(s)
// jQuery(elem).attr(attr,eval("elm"+attr));
jQuery(elem).$(s)(attr,eval("elm"+attr));//i tried this.

how to assign a variable name in the above code(in place of s) so that i need to add an attribute to the element "elem".

Upvotes: 0

Views: 1982

Answers (2)

Ajithlal
Ajithlal

Reputation: 1

This will help you

Without variable

alert($('.view-character-navigation li a[href="shows/angelina-ballerina"]').attr("href"));

With variable

showurl = '/shows/angelina-ballerina';
alert($('.view-character-navigation li a[href="'+showurl+'"]').attr("href"));

Upvotes: -1

BenjaminRH
BenjaminRH

Reputation: 12182

Your code has several problems:

  1. You've left off end-of-line semicolons (lines 1 & 2)
  2. Your code is illogical: you seem to be trying to use jQuery to get an attribute, not an element (line 2)
  3. Your jQuery code is completely invalid (line 4)

I'm not quite sure exactly what you're trying to do, but here's how you would use jQuery to get an element and then alter that element's attributes using jQuery's attr() method:

var myElement = $('#myElementID'); // Store the element in a variable with jQuery
$(myElement).attr('attr', 'value'); // Set/alter one of the element's attributes

Upvotes: 3

Related Questions