0
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".

1

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

-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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.