2

I know a little bit jQuery, and even though it is still javascript, I do not know how to convert the following code to pure Javascript:

$('#breadcrumbs .category[href="http://asksomeone.co.za/category/products"]')
    .attr('href','http://myurl.com/803-2');

How would I do that?

The reason why I am doing this is because I am using a Wordpress theme...whenever I include the jQuery library, it breaks many things on my site, when I don't include the jquery library, no jquery code runs...

6

5 Answers 5

2

$ is just an alias for jQuery. You could just use:

jQuery('#breadcrumbs .category[href="http://asksomeone.co.za/category/products"]').attr('href','http://myurl.com/803-2');

or else wrap the code in a self executing function passing in jQuery like:

(function($){
     $('#breadcrumbs .category[href="http://asksomeone.co.za/category/products"]').attr('href','http://myurl.com/803-2');
})(jQuery);

If you really want to go down the pure js route, then take a look at:

document.getElementById()

and

element.setAttribute()
Sign up to request clarification or add additional context in comments.

Comments

2

If you're willing to drop support for Internet Explorer 7, you could do

var i, elems = document.querySelectorAll('#breadcrumbs .category[href="http://asksomeone.co.za/category/products"]');

for (i = 0; i < elems.length; i++) {
   elems[i].href = 'http://myurl.com/803-2';
}

Otherwise, I'm afraid you are better off using a library.

2 Comments

You're welcome, glad you found the real source of the problem.
Yes...it was as simple as wrapping my code inside: jQuery(function ($) {});...But knowing how to do it in pure js is good to know anyways...
2

As others have mentioned, you might want to just try to resolve the library conflicts.

To answer your question, here is a possibility:

var elements = document
    .getElementById('breadcrumbs')
    .getElementsByClassName('category'); //Not supported below IE9 :-(

for (var i = 0; i < elements.length; i++) {
    if (elements[i].href == "http://asksomeone.co.za/category/products") {
        elements[i].setAttribute('href', "http://myurl.com/803-2");
    }
}

Comments

1

jQuery's selectors are based on sizzle: http://sizzlejs.com/ Maybe you can include that library? If not, the first part of your problem is quite difficult.

The second part is easy:

myElement.setAttribute('href','http://myurl.com/803-2');

Comments

0

If you don't like to include jQuery library for url change. We need following functions

function getByClassName( className , selector ) {
    selector = selector || document;

    if( selector.getElementsByClassName ) {
        return selector.getElementsByClassName( className );
    }

    var els = [];

    var childs = selector.getElementsByTagName( '*' );

    for( var i = 0, j = childs.length; i < j; i++ ) {
        if( childs[i].className.indexOf( className ) > -1 ) {
            els.push( childs[i] );
        }
    }
    return els;
};

function changeUrl( els ,  url , newUrl  ){

    for( var i = 0; i < els.length ; i++ ){

        if ( els[i].href == url ) {
                els[i].href = newUrl ;
        }
    }

}

and call the function

changeUrl ( 
  getByClassName( 'category' , document.getElementById('breadcrumbs')  ) , 
  "http://asksomeone.co.za/category/products" , 
  "http://myurl.com/803-2"
);

or If you are using jQuery library .. use this

(function( JQ ){

    JQ('#breadcrumbs .category[href="'
        + 'http://asksomeone.co.za/category/products"]')
       .attr('href',"http://myurl.com/803-2");

})( jQuery );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.