fbpx
Welcome, Guest
Username: Password: Remember me

TOPIC: Can't get my JS to work on a simple link

Can't get my JS to work on a simple link 1 month 3 weeks ago #30477

  • Whales
  • Whales's Avatar
  • Offline
  • New Member
  • Posts: 5
  • Karma: 0
Hi,

I just discovered Astroid and I love it.

I just launched my website : https://lise-whales.com/

Joomla 3.9.28
Template One 1.3.2
Astroid Framework 2.5.2

I want to use Featherlight lightbox with JS script

But before that, I'm trying to understand how use basic JS
I'm working on a basic example from Stackoverflow

On my home page I have this link whick works fine alone and links to my second page
<a id="ali" alt="Ali" href="https://lise-whales.com/page-fl">Link</a>

Then, when I place in the JS custom code to make my ID "ali" to change the href to stackoverflow web site, it doesn't work
$(document).ready(function() {
$("#ali").attr("href","https://stackoverflow.com");
});

What I am doing wrong ?

Where should I put my JS code ?

Thanks

Lise
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 3 weeks ago #30478

  • Whales
  • Whales's Avatar
  • Offline
  • New Member
  • Posts: 5
  • Karma: 0
My code in Astroid
Attachments:
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 3 weeks ago #30497

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Posts: 4489
  • Thank you received: 646
  • Karma: 115
Hi,

Try user jQuery instead of $ and putting it under document.ready.

See this url for more info:
api.jquery.com/ready/

Thanks,
Chetan
Thank You,
If you like our support and products, tweet us at joomdev and let the world know about it.
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 3 weeks ago #30499

  • Whales
  • Whales's Avatar
  • Offline
  • New Member
  • Posts: 5
  • Karma: 0
Hi,

well I completely lost.

I already used document.ready and I thought I was already using jQuery.

Could you give me an example of what you are referring to ?

Thanks

Lise
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 3 weeks ago #30500

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Posts: 4489
  • Thank you received: 646
  • Karma: 115
Hi Lise,

I am sorry but this is outside the support scope. You have to know how to use basic JavaScript/jQuery and you should be able to implement this easily.

Thanks,
Chetan
Thank You,
If you like our support and products, tweet us at joomdev and let the world know about it.
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 3 weeks ago #30507

  • Whales
  • Whales's Avatar
  • Offline
  • New Member
  • Posts: 5
  • Karma: 0
Hi,

I return with the answer to my question.

I asked it on joomla.stackexchange and got the answer.

I replace the $ with jQuery and place this in the JS Custom and even in a custom JS file (monjs.js) that I call in the before head area but it didn't work either :
jQuery(document).ready(function() {
jQuery("#ali").attr("href","https://stackoverflow.com");
});

The solution was to convert it in JavaScript :
document.addEventListener('DOMContentLoaded', () => {
  document.getElementById('ali').setAttribute('href', 'https://stackoverflow.com');
});

It worked perfectly either in the JS custom area or in with my custom JS file (monjs.js) that I call in the before head like this :
<!-- Mon JS -->
<script type="text/javaScript" src="/media/system/js/monjs.js"></script>
<!-- End - Mon JS -->

Looks like I can't even call simple jQuery scripts. I have to convert it to JavaScript. :blink:

Well, that's something I'm going to learn and try. ;)

Have a good day

Lise
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 3 weeks ago #30517

  • Whales
  • Whales's Avatar
  • Offline
  • New Member
  • Posts: 5
  • Karma: 0
Hi,

I get back to you because I found another solution to solve my problem AND use jQuery script.

I placed
JHtml::_('jquery.framework');

inside <?php ?> before 'head' in the index.php of my template
<?php

/**
 * @package   Astroid Framework
 * @author    JoomDev https://www.joomdev.com
 * @copyright Copyright (C) 2009 - 2020 JoomDev.
 * @license https://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
 */
// No direct access.
defined('_JEXEC') or die;
defined('_ASTROID') or die('Please install and activate <a href="https://www.astroidframework.com/" target="_blank">Astroid Framework</a> in order to use this template.');

if (file_exists(__DIR__ . "/helper.php")) {
   require_once __DIR__ . "/helper.php"; // Template's Helper
}

JHtml::_('jquery.framework');

$document = Astroid\Framework::getDocument(); // Astroid Document
// Output as HTML5
$this->setHtml5(true);
?>
<!DOCTYPE html>
<html lang="<?php echo $this->language; ?>" dir="<?php echo $this->direction; ?>">

<head>
...
</head>

</html>

This way, I can use jQuery scripts directly inside my custom JS file monjs.js
jQuery(document).ready(function(){
jQuery("#ali").attr("href","https://stackoverflow.com");
});

Good Day

Lise
The administrator has disabled public write access.

Can't get my JS to work on a simple link 1 month 2 weeks ago #30560

  • chandandeep
  • chandandeep's Avatar
  • Offline
  • Administrator
  • Posts: 4593
  • Thank you received: 648
  • Karma: 116
Am happy your problem is solved.
Thank You,
If you like our support and products, tweet us at joomdev and let the world know about it.

To know more about products check out the their documentation.
The administrator has disabled public write access.
Moderators: chandandeep
Time to create page: 0.420 seconds
Cron Job Starts