1

I'm trying to get the value of an input text field using

var $rel = '#'+$(this).attr('rel');

but it returns nothing when I test it with

alert($rel.val());

Example: http://jsfiddle.net/59VmX/

2
  • 1
    Just use this.rel instead of $(this).attr('rel') and wrap it in jquery to get val(). Commented Sep 5, 2013 at 17:48
  • 1
    Don't forget to include the jquery library in the Frameworks & Extensions when using jQuery with jsFiddle. Commented Sep 5, 2013 at 17:52

3 Answers 3

5
'#'+$(this).attr('rel');  // This is just a string

supposed to be

$('#'+$(this).attr('rel'));  OR // $('#'+ this.rel);

You need to encase it as a jQuery object if you want to use val method on the object

Sign up to request clarification or add additional context in comments.

7 Comments

Or even better: $('#' + this.rel)
@Itay. According to the code the OP provided, I believe he was trying to access a input with id of that rel attribute.
When I try to do an alert to test it, there is no value. Updated: jsfiddle.net/59VmX/6
That is because you do not have a input that has the id set to the rel attribute
Check this jsfiddle.net/59VmX/8.. Also make sure you select the jQuery framework in options on left.
|
1

Remove the .val() Try just alert($rel);

Comments

0

Here is a working fiddle , In your old fiddle ,jquery is not loaded and:

Instead of this :

 var $rel = '#'+$(this).attr('rel');
 alert($rel.val());

It should be :

 var $rel = $(this).attr('rel');
 alert($rel);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.