• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

jQuery attribute to ID

Hello Experts,

Can anyone tell me why I am getting "[object Object]" instead of e.g. #hrefAttribute".
Basically i am trying to get the "href" attribute and convert it to an ID selection in the jQuery.

$("#news-menu a").click(function(event) {
		event.preventDefault();
		var textSection = $(this).attr('href');
		var textTarget = 	$('#'+textSection+'');		
		console.log (textTarget);
	});

Open in new window

0
Refael
Asked:
Refael
  • 2
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
$("#news-menu a").click(function(event) {
		event.preventDefault();
		var textSection = $(this).attr('href');
		var textTarget = 	$('#'+textSection+''); // this return a JQuery Object : "[object Object]" 
var textTarget = 	$('#'+textSection+'').html(); // try this OR
var textTarget = 	$('#'+textSection+'').text(); // this
		console.log (textTarget);
	});

Open in new window

0
 
RefaelAuthor Commented:
Hi leakim971

I do not get it right using the html or text conversion.
Yet i have tried removing the "$" and it looks like it works!?

var textTarget = 	('#'+textSection+'');	

Open in new window

0
 
leakim971PluritechnicianCommented:
var textSection = "xyz";
var textTarget = 	'#'+textSection; // string, selector
alert(   $(textTarget).text()  ); // will alert "hello world"

Open in new window



<div id="xyz">hello world</div>
0
 
RefaelAuthor Commented:
Thank you! Now it is clear. So by removing the "$" I could get the string and pass the ID.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now