Benyi Benyi - 1 year ago 55
HTML Question

.attr() as a variable in attribute selector not work

I have code like this

<div class="field">
<input id="post-titie" type="text">
<div class="ui olive message" for-field="post-title" hidden>
<div class="header">About Title</div>
<ul class="list">
<li>Visitors can see your title even if they're not logged in.</li>
<li>The title is public.</li>

$('.form textarea, :input').focus(function(){

var forwhom = $(this).attr('id'); /* not working */
/* var forwhom = 'post-title'; works */

var _msg = $(this).parents().eq(2).find('div[for-field="'+forwhom+'"]');
$(_msg).transition('fade down').transition('show');


What I want to do is when
focused, show messages div with

I make
as a variable (eg.
), and find it with
. This is not working.

But If I replace
var forwhom
(jQ selector) to a absolute value like
, It does work.

It's strange! According to jQuery documentation, the
method returns a string not an object.

What is the problem?

Answer Source

Div attribute for-field="post-title" and input has post-titie there is spelling mistake.

post-title and post-titie are not same that's why your code is not finding the div with for attribute.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download