LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2013, 05:17 PM   #1
babbab
Member
 
Registered: Mar 2010
Distribution: slackware64 current
Posts: 104

Rep: Reputation: 4
javascript document.queryselector


suppose links is array with href value

can i do

document.querySelector('a[href="' + links[1] + '"]').click();

to click on selected element?
 
Old 01-25-2013, 05:54 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You know, you could have just tried it out:

Code:
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Click Selector</title>
	</head>
	<body>
		<a href="http://duganchen.ca/">Dugan Chen's homepage</a>
		<script>
			(function () {
				'use strict';
				var links = ['', 'http://duganchen.ca/'];
				document.querySelector('a[href="' + links[1] + '"]').click();
			}());
		</script>
	</body>
</html>
It works for me.
 
Old 01-25-2013, 06:21 PM   #3
babbab
Member
 
Registered: Mar 2010
Distribution: slackware64 current
Posts: 104

Original Poster
Rep: Reputation: 4
I'm using casperjs. and doesn't work for me.

Code:
function getLinks() {
    var links = document.querySelectorAll('a[href^="inquiryDetail.jis?caseId="]');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href')
    });
}

document.querySelector('a[href="' + links[1] + '"]').click();
can someone provide me a link about what the mysterious function getLinks() does?
I know it stores links starts with 'a[href^="inquiryDetail.jis?caseId="]' to links
but the rest of the function is still mysterious to me.
this part is

Code:
return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href')
but this code works

Code:
this.echo(links[1]);
and this is error msg

Code:
TypeError: 'null' is not an object (evaluating 'document.querySelector('a[href="
' + links[1] + '"]').click')

Last edited by babbab; 01-25-2013 at 06:34 PM.
 
Old 01-25-2013, 07:20 PM   #4
babbab
Member
 
Registered: Mar 2010
Distribution: slackware64 current
Posts: 104

Original Poster
Rep: Reputation: 4
Code:
this.click('a[href="' + links[1] + '"]');
did the job
my intention was select the element and clicking it
somehow queryselector didnt choose the element
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
html; character encoding per document tag, not whole document TheLinuxDuck Programming 0 08-14-2008 11:12 AM
JavaScript - document.getElementsById Kamikazee Programming 2 10-29-2007 04:23 PM
javascript document.links always is cero poeta_boy Programming 1 11-23-2006 10:30 AM
JavaScript question - how to save a created document within browser davidnye Programming 1 09-05-2006 11:36 AM
Convert openoffice document into ms document through cli? bruno buys Linux - Software 0 07-05-2004 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration