Learn to implement height and width in Jquery functionality

Recently, I came across a jQuery script online that enhances the functionality of my ticketing software by allowing me to add videos to a wiki. However, there is an issue with the code as it doesn't specify a height or width for the video. Is there a way to modify the code to include these dimensions?

if ($('#idEditArticle')) {
    var videos = $('a[href$=".m4v"], a[href$=".mp4"]');
    $.each(videos, function(i, video) {
        $(video).parent().prepend('<video src="'+$(video).attr('href')+'" controls></video><br/>');
    });
}

Below is the HTML output:

<p>
<a border="0" class="fb_attachment" href="default.asp?pg=pgDownload&amp;pgType=pgWikiAttachment&amp;ixAttachment=136818&amp;sFileName=Paragon%20Invoice%203.mp4" rel="nofollow" title="">Paragon Invoice 3.mp4</a></p>

Although manually adding height and width attributes to the HTML seems feasible, I encountered difficulties in applying inline CSS styles directly to the elements. Wrapping the content in a div didn't work either, as it deleted the style upon submission.

Is it possible to incorporate height and width specifications into the jQuery code to automatically set these values for the videos?

Answer №1

This solution is effective. Please keep in mind that I am utilizing the max-width property in this example, but any other CSS style can be used.

if ($('#idEditArticle')) {
    var videos = $('a[href$=".m4v"], a[href$=".mp4"]');
    $.each(videos, function(i, video) {
        // Added a styling attribute here.
        $(video).parent().prepend('<video src="'+$(video).attr('href')+'" controls style="max-width: 100%;"></video><br/>');
    });
}

A more straightforward approach (from a coding perspective) would be:

if ($('#idEditArticle')) {
    // Select all matching elements and return an array of jQuery objects.
    var videos = $('a[href$=".m4v"], a[href$=".mp4"]');
    // Use the videos.each method to iterate through the preceding array.
    videos.each(function(){
        // create link, video, and source elements
        var link   = $(this);
        var video  = $('<video />');
        var source = $('<source />').attr('src', link.attr('href'));
        // correctly append the elements to build a tree structure
        video.append(source);
        // Apply multiple styling attributes here
        video.css({'max-width':'100%'});
        // prepend the tree to the desired location
        link.parent().prepend(video);
    });
}

The implementation works well (there may have been an additional space in < source /> - it should actually be <source />:

        // Select all matching elements and return an array of jQuery objects.
        var videos = $('a[href$=".m4v"], a[href$=".mp4"]');
        // Use the videos.each method to iterate through the preceding array.
        videos.each(function(){
            // create link, video, and source elements
            var link   = $(this);
            var video  = $('<video />');
            var source = $('<source />').attr('src', link.attr('href'));
            // correctly append the elements to build a tree structure
            video.append(source);
            video.css('max-width','100%');
            // prepend the tree to the desired location
            link.parent().prepend(video);
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<a href="test.mp4">Test</a>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rotate the horizontal scroll of an inner div within an outer div using CSS

I am looking to create an outer div with horizontal scroll and inner div with vertical scroll where the inner div moves along with the outer div. <body> <div id="wrapper"> <div id="outer">content outer <div id="inner"&g ...

"Error" - The web service call cannot be processed as the parameter value for 'name' is missing

When using Ajax to call a server-side method, I encountered an error message: {"Message":"Invalid web service call, missing value for parameter: \u0027name\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(O ...

The correlation among the event loop, libuv, and the V8 engine

Exploring the intricacies of Node.js architecture has led me to several thought-provoking questions. Is the event loop a component of libuv or v8? Does the event queue operate within the event loop? And if so, is it generated by libuv, v8 engine, or th ...

Why is DIV centered in desktop browsers but not on iPhone?

Hello there! Currently, I am working on a basic webpage that hosts an AWeber signup form with the intention of making it mobile-friendly. You can view it here: When I view the page on my MacBook, the div containing the form is centered properly; however, ...

Why does it appear that Angular is undefined in this straightforward Angular demonstration?

I'm completely new to AngularJS and I've encountered an issue. Yesterday, I ran a very simple AngularJS application that I downloaded from a tutorial and it worked perfectly. The application consists of 2 files: 1) index.htm: <!DOCTYPE htm ...

POST requests in Angular Universal are making use of the IP address assigned to my server

My Angular Universal application (version 5.2.11) is currently hosted on Heroku, running on a Node server using express. I have implemented rate-limiters in all my POST routes to restrict requests by IP address, checking the request's IP through req.h ...

Tips for preventing odd symbols from appearing before the query in Firefox

While working on a new website, I've been using jquery for various functionalities and it's been quite magical. However, I've noticed that when I load jquery 1.4.2 in Firefox, there are sometimes strange characters appearing in front of the ...

Steps for building HTML based on the provided layout

I am looking to dynamically create the following HTML structure: <tr> <td><label class="mt-checkbox mt-checkbox-outline"><input type="checkbox" class="chkclass">A<span></span></label></td> <td&g ...

Unresolved issue with Flask failing to retrieve URL argument from AJAX request

I'm having trouble with retrieving a submitted string from the server using Flask's Request module. The URL arguments don't seem to be picked up correctly, resulting in receiving "HELLO" instead of the actual string variable. Additionally, ...

Updating values in an Object by assigning them with results from an array

I've been grappling with a data structure issue in nodejs and I could really use some assistance. Here's the scenario: I have an object: let obj = { commenter: '', comment: '', numberOflikes: 0, } Along with a containe ...

Difficulty retrieving JSON object with AngularJS

I am having trouble extracting information from a JSON string that I have (see image below). { all: [{ info: { name: "Fahad", lat: "41.954815", lon: "-87.647209" } }, { info: { ...

Issue with Nav Bar Toggle not changing text to white color

Query I'm baffled as to why the text in the navigation bar (when opened with the Burger Menu) refuses to turn white. I've exhausted all efforts to change the color to white. Please assist me in pinpointing why the text remains unaffected when t ...

Using the increment operator within a for loop in JavaScript

this code snippet causes an endless loop for (let i = 0; ++i;) { console.log(i) } the one that follows doesn't even run, why is that? for (let i = 0; i++;) { console.log(i) } I want a thorough understanding of this concept ...

What is the best way to connect a fresh post to a different URL in ReactJS and Redux?

Currently, I am working with Redux and ReactJS to develop a discussion platform. One of the key features I am trying to implement is where users can input the title and content of their post, which will then be submitted to create a new post. After submit ...

The closure of the Mongoose connection is triggered by the update of the node

Currently in the process of updating packages from a previous project and also upgrading Node. I have successfully upgraded from Node 12 to Node 18, moving up 2 versions at a time. Production appears to be functioning normally, however, in my development ...

The body can only stretch up to a certain percentage of its width, not a full 100%

Recently, I've been encountering an issue with my webpage where the body does not stretch 100% the width of the screen. I've tried numerous CSS rules to fix it, but nothing seems to be working. When you view the page in a browser, it appears to c ...

Modify the DOM element stored in a variable before inserting it

Currently, I am using a function that appends elements to the page by using a template. The code snippet looks like this: var template = '<div class="offer-container"><a class="link" href="${link}" target=" ...

Tips for utilizing the simple-peer module within a Node.js environment?

I recently started using Node.js and I'm interested in incorporating the simple-peer module into my application. However, I am struggling to understand how to implement it based on the provided documentation. Are there any resources available that can ...

How do you typically approach testing Cloud Code on Parse?

While working on developing a substantial amount of business logic in webhooks like beforeSave/afterSave/etc. using Parse.com, I have encountered some challenges as a JavaScript/Parse beginner. The process seems somewhat tedious and I'm questioning if ...

The div has extra white space at the bottom due to the Hide/Show content feature

Having trouble stretching a scrolling div to 100% of its parent container's height? The Hide/Show content feature is causing whitespace at the bottom of the div. Check out the jsfiddle here: http://jsfiddle.net/fkvftff2/1/ LATEST UPDATE: The issue a ...