jQuery: The Write Less, Do More JavaScript Library

Why does 'attr' work with 'divs' and not 'p's'?

View: New views
3 Messages — Rating Filter:   Alert me  

Why does 'attr' work with 'divs' and not 'p's'?

by dorich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Using the following code

jquery
var originalTitle=$("a.basic").attr("title");
                        $("div.area1").text(originalTitle);

html
<p>This is where the original title should go</p>
<div class="area1"></div>

I get the expected title attribute written into the 'div'.

However, if I swap the 'div' for a 'p'  the result is that the 'href'
is written into the paragraph instead of the title. Here is the
modified code that produces the unexpected result:

jquery
var originalTitle=$("a.basic").attr("title");
                        $("p.area1").text(originalTitle);

html
<p>This is where the original title should go</p>
<div class="area1"></div>


I suspect the problem is cockpit error but I wondered if there is some
restriction on this combination of jquery that requires a 'div' for
the area where the text is written.

Alternatively is someone can point out my error that would be much
appreciated.

Thanks.

Re: Why does 'attr' work with 'divs' and not 'p's'?

by D A-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> jquery
> var originalTitle=$("a.basic").attr("title");
>                        $("p.area1").text(originalTitle);
>
> html
> <p>This is where the original title should go</p>
> <div class="area1"></div>

Your selector is looking for a paragraph with a class of 'area1'.
However, there is no paragraph with that class in your markup.

-DA

Solved: Re: Why does 'attr' work with 'divs' and not 'p's'?

by dorich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the response.
I pasted the wrong code, they were consistent in the original. mea
culpa!
However, this is solved. I had created a conflict with classes
somewhere else in the html and that was the source of the problem.

On Nov 10, 12:12 pm, D A <dali...@...> wrote:

> > jquery
> > var originalTitle=$("a.basic").attr("title");
> >                        $("p.area1").text(originalTitle);
>
> > html
> > <p>This is where the original title should go</p>
> > <div class="area1"></div>
>
> Your selector is looking for a paragraph with a class of 'area1'.
> However, there is no paragraph with that class in your markup.
>
> -DA